Weedpacket;11025001 wrote:No, the problem is that you have a syntax error on line 20. Which would appear to be centred on the fact that your idea about how [man]global[/man] works is back to front.
I fixed the global issue but something is still wrong. Can i use the global $mysqli for both functions? I am not sure on that since they are AJAX functions and both could be called while the user is still on the page. Should I close the mysqli at the end of the function? I tried creating the new mysqli inside each function. Then I tried it with a global mysqli inside each function. This is the error message.
Applications/MAMP/htdocs/todo/form_handler_copy1.php on line 39
[12-Mar-2013 00:34:53 Europe/Berlin] PHP Notice: Undefined variable: dbpass in /Applications/MAMP/htdocs/todo/form_handler_copy1.php on line 5
[12-Mar-2013 00:34:53 Europe/Berlin] PHP Notice: Undefined variable: port in /Applications/MAMP/htdocs/todo/form_handler_copy1.php on line 5
[12-Mar-2013 00:34:53 Europe/Berlin] PHP Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /Applications/MAMP/htdocs/todo/form_handler_copy1.php on line 5
I looked on phpinfo. It says mysqli socket, MYSQLI_SOCKET /Applications/MAMP/tmp/mysql/mysql.soc
Does that mean I need to add the socket? Why do I get undefined variable dbpass and port? It says I am not using a password but I am. I was able to log in with MySQL Workbench to the server so I have the right passord and login.
$dbhost = 'localhost';
$dbpass = 'root';
$dbuser = 'root';
$dbname = 'taskos';
$port = 8889;
$mysqli = new mysqli($dbhost,$dbuser, $dbpass, $dbname,$port);
if(isset($_GET['task_todo'])){
set_it();
}else{
get_it(); }
function set_it(){
global $mysqli;
if ($mysqli->connect_error) {
printf("Connect failed:%s\n", mysqli_connect_error());
exit();
}
$task = $_GET['task_todo'];
$name = $_GET['todo_name'];
$date = $_GET['due_date'];
$task = $mysqli->real_escape_string($task);
$name = $mysqli->real_escape_string($name);
if(
$mysqli->query("INSERT INTO taskos.task(todo, name,date)
VALUES('$task','$name','$date')") )
{ printf(" $task, $name ,$date\n", $mysqli->affected_rows ); }
}
function valid_date($date) {
return (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date));
}
function get_it(){
global $mysqli;
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
echo 'Success... ' . $mysqli->host_info . "\n";
$sql = 'SELECT todo,date,name,id FROM task WHERE todo IS NOT NULL || todo > "" ' ;
$sqlOrder = 'Order by Date' ;
$sql .= $sqlOrder ;
$result = $mysqli->query($sql);
if($result = $mysqli->query($sql)) {
//$todoArray = array();
$i= 0;
while($row = $result->fetch_object()) {
// printf("%fs %s %s \n",$obj->todo, $obj->date, $obj->name, $obj->id);
$function_result[$i]= $row;
$i++;
} //end while
printf(json_encode($function_result));
}//end if
}
by the way I can successfully connect using mysql_connect test page but I thought that was deprecated and I'm using mysqli .
$dbhost = 'localhost';
$dppass = 'root';
$dbuser = 'root';
$link = mysql_connect($dbhost,$dppass,$dbuser);
mysql_select_db('taskos');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$query = "SELECT * FROM task";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result))
{
foreach($line as $value)
{
print $value."\n";
}
}
mysql_close($link);