I've already asked a question on this code I'm working on, just not about the same problem. Either way sorry for the repost!
So I'm having trouble with the code, as follows:
<?php
// Create connection
$host = "localhost";
$username="tudor";
$password="passw0rd";
$con=mysqli_connect($host, $username, $password);
if(! $con )
{
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br />';
$db_1 = mysqli_select_db( $con, 'db_1' );
if (! $db_1) {
die('Could not select database: ' . mysqli_error());
}
else {
echo "Database successfully selected<br />===============================<br />";
}
//===================================
$a = 1;
$b = 2234;
$table = "CREATE TABLE info (id INT NOT NULL AUTO_INCREMENT, city CHAR(40), country CHAR(40))";
if (! $table) {
die('Could not create table ' . mysqli_error($con));
}
else {
echo "Table created<br />";
}
$insert = "INSERT INTO info (city, country) VALUES ($a, $b)";
if (! $insert) {
die('Could not insert ' . mysqli_error($con));
}
else {
echo "Inserted<br />";
}
$select = "SELECT * FROM info";
$result = mysqli_query ($con, $insert);
if (! $result) {
die('Result not working ' . mysqli_error($con));
}
else {
echo "Result working<br />";
}
echo "result: ".$result['city']. " ";
mysqli_close($con);
?>
This outputs (blockquote doesn't display page breaks):
Connected successfully Database successfully selected
=============================== Table created Inserted Result not working Table 'db_1.info' doesn't exist
What does it mean by "Table 'db.info'" not existing? It clearly says that my info table was created...
What I tried doing is inverting the variables in the $result query: $result = mysqli_query ($insert, $con);, because I had seen that syntax in a book. However all it gave was the following message in the output:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given
in C:\wamp...
Thoughts anyone? Thanks in advance!
Edit: really appreciate the help everyone, thanks a lot!
ok. Here is your code.
if(! $con )
{
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br />';
if (!$con) {trigger_error("Could not connect to MySQL: " . mysqli_connect_error()); }
else { echo "Database successfully connected<br />===============================<br />"; }
$a = 1;
$b = 2234;
$table = mysqli_query($con,"CREATE TABLE IF NOT EXISTS info (`id` int(11) unsigned NOT NULL auto_increment,
`city` CHAR(40),
`country` CHAR(40), PRIMARY KEY (`id`) )ENGINE=MyISAM DEFAULT CHARSET=utf8");
if (!$table) {
die('Could not create table ' . mysqli_error($con));
}
else {
echo "Table created<br />";
}
$insert = mysqli_query ($con,"INSERT INTO info (city, country) VALUES ('$a', '$b')");
if (!$insert) {
die('Could not insert ' . mysqli_error($con));
}
else {
echo "Inserted<br />";
}
$select = mysqli_query ($con,"SELECT * FROM info");
$res=mysqli_fetch_array($select);
if (! $res) {
die('Result not working ' . mysqli_error($con));
}
else {
echo "Result working<br />";
}
echo "result: ".$res['city']. " ";
echo "result: ".$res['country']. " ";
mysqli_close($con);
You are not doing a mysqli_query()
on $table
before your mysqli_query()
on $insert
, and you are not doing a mysqli_query()
on $select
$table = "CREATE TABLE info (id INT NOT NULL AUTO_INCREMENT, city CHAR(40), country CHAR(40))";
if (! $table)
$insert = "INSERT INTO info (city, country) VALUES ($a, $b)";
if (! $insert) {
$select = "SELECT * FROM info";
$result = mysqli_query ($con, $insert);
if (! $result)
try adding the mysqli_query()
-
$table_sql = "CREATE TABLE `info` (`id` INT NOT NULL AUTO_INCREMENT, `city` CHAR(40), `country` CHAR(40), PRIMARY KEY (`id`))";
$table = mysqli_query ($con, $table_sql);
if (! $table) {
die('Could not create table ' . mysqli_error($con));
}
else {
echo "Table created<br />";
}
$insert_sql = "INSERT INTO `info` (`city`, `country`) VALUES ('$a', '$b')";
$insert = mysqli_query ($con, $insert_sql);
if (! $insert) {
die('Could not insert ' . mysqli_error($con));
}
else {
echo "Inserted<br />";
}
$select = "SELECT * FROM `info`";
$result = mysqli_query ($con, $select);
if (! $result) {
die('Result not working ' . mysqli_error($con));
}
else {
echo "Result working<br />";
}
Edit
Also, this line will fail -
echo "result: ".$result['city']. " ";
as you have to fetch the array from the query using mysqli_fetch_array()
$results = mysqli_fetch_array($result);
echo "result: ".$results['city']. " ";
Table 'db_1.info' doesn't exist
Means, that table info
does not exist in db db_1
so, make sure if that is the case.