I'm trying to learn more about MySQL databases and writing custom PHP scripts.
I've queried the WordPress database before and I'm comfortable adding custom tables, custom queries etc. But I've never created my own database before.
So far I've got a db called simplecms
, with one table called core
which has two columns, core_name
and core_value
. So far it has one row in it.
All I'm trying to do it echo out the value of this one row.
So far, I have this code:
[updated code]
<?php
// New Connection
$mysqli = new mysqli('localhost','root','root','simplecms');
// Check for errors
if( mysqli_connect_errno() ) {
echo mysqli_connect_error();
} else {
echo('connected to db...<br /><br />');
}
// Create Query
$query = "SELECT core_value FROM core WHERE core_name='url'";
// Execute Query
if( $result = $mysqli->query($query) ) {
// Cycle through results
while($row = $mysqli->fetch_object($result)){
echo $row->column;
}
// Free result set
$result->close();
} else {
printf("Error message: %s
", $mysqli->error);
}
// Close connection
$mysqli->close();
?>
This returns: Error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'url\'' at line 1
Any ideas where I might be going wrong?
[update]
I've managed to get it working with some completely new code! I'm not 100% why this works, but here it is just incase:
<?php
// new Connection
$mysqli = new mysqli('localhost','root','root','simplecms');
// check for errors
if( mysqli_connect_errno() ) {
echo mysqli_connect_error();
} else {
echo('connected to db...<br /><br />');
}
// create a prepared statement
if($query = $mysqli->prepare("SELECT core_value FROM core WHERE core_name='url'")) {
// execute
$query -> execute();
// bind results
$query -> bind_result($result);
// fetch value
$query -> fetch();
// echo out results
echo $result;
// close the statement
$query -> close();
}
// close mysqli
$mysqli -> close();
?>