Welcome to WebmasterWorld Guest from 23.23.53.177

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

mysql

trying to learn mysqli

   
8:31 pm on Aug 4, 2013 (gmt 0)



Confused! Trying to learn mysqli
Here's my code:
<?php
$con=mysqli_connect("localhost","root","","tutorials");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$first='Glenn';
$last="Quagmire";
$age=33;

mysqli_query($con,"INSERT INTO players (FirstName, LastName, Age)
VALUES ('rickie', 'sumrall',72)");

mysqli_query($con,"INSERT INTO players (FirstName, LastName, Age)
VALUES ('$first', '$last', '$age')");

if (!mysqli_query($con,$mysqli_query))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>
the code:
mysqli_query($con,"INSERT INTO players (FirstName, LastName, Age)
VALUES ('Jerry', 'Jones',72)"); successfully inserted into the db table.
the code:
$first='Glenn';
$last="Quagmire";
$age=33;

mysqli_query($con,"INSERT INTO players (FirstName, LastName, Age)
VALUES ('$first', '$last', '$age')");
resulted in the following error and no entry in the table:
Warning: mysqli_query() [function.mysqli-query]: Empty query in C:\xampp\htdocs\diff\insert.php on line 19
Error:

Help please
8:52 pm on Aug 4, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



if (!mysqli_query($con,$mysqli_query)) 


The variable $mysqli_query is not defined anywhere in the code you provided.
9:26 pm on Aug 4, 2013 (gmt 0)



It's here:
mysqli_query($con,"INSERT INTO players (FirstName, LastName, Age)
VALUES ('$first', '$last', '$age')");

if (!mysqli_query($con,$mysqli_query))
{
die('Error: ' . mysqli_error($con));
}
and also in the code, if I'm understanding your meaning by defined.
9:30 pm on Aug 4, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Calling the function mysqli_query does not create the variable $mysqli_query.

You're trying to use a variable before you defined it, and PHP being the forgiving language that it is, is allowing it to be interpreted as an empty string for the mysqli_query call you have in your if brackets.

By defined I mean you haven't done this:
$mysqli_query = 

If you put error reporting on E_ALL, you would find you would get a message like:

Notice: Undefined variable: mysqli_query in /path/to/file.php on line 19
9:54 pm on Aug 4, 2013 (gmt 0)



I'm really sorry for being so dense but I have to search for code snippets to fill out "my" code and have found none that uses the $mysqli_query = , if not too much trouble, could you correct my code and explain what you did so I can learn? mysqli is so confusing ... I liked the old mysql.
10:26 pm on Aug 4, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Humm, I'm not sure what you're actually trying to do with the problem line, so simply fixing your code for you is not even possible. Anyway. The problem line. You're referencing the variable $mysqli_query when you have not created that variable:

if (!mysqli_query($con,
$mysqli_query
)) 

As for the confusion, mysqli can be used exactly the same way as mysql, just many of the functions that had an optional parameter for $connection at the end now have a required one at the start.

Also bear in mind, there is a built in PHP class to use mysqli which greatly simplifies the syntax (No need to keep that $connection variable hanging around)

$mysqli = new mysqli($hostname, $username, $password, $database);

$sql = "insert into my_table (my_column) values ('some value')";
if(!$mysqli->query($sql)) {
die($mysqli->error);
}

$sql = "select * from my_table order by my_column asc";
$result = $mysqli->query($sql);
if(!$result) {
die($mysqli->error);
}

if($result->num_rows == 0) {
echo 'No results';
} else {
while($row = $result->fetch_assoc()) {
echo "<div>{$row['my_column']}</div>";
}
}

$mysqli->close();

[php.net...]