homepage Welcome to WebmasterWorld Guest from 50.17.107.233
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
mysql
trying to learn mysqli
ricksum




msg:4599296
 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

 

Readie




msg:4599298
 8:52 pm on Aug 4, 2013 (gmt 0)

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

The variable $mysqli_query is not defined anywhere in the code you provided.

ricksum




msg:4599311
 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.

Readie




msg:4599317
 9:30 pm on Aug 4, 2013 (gmt 0)

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

ricksum




msg:4599323
 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.

Readie




msg:4599335
 10:26 pm on Aug 4, 2013 (gmt 0)

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...]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved