Ok, so I've been informed that it would be best practice to convert over to the new mysqli
So I've been working on this on a new site, so far so good, but I ran into a problem where I can't figure out how to convert it for my search query
I have a search feature added to my site, but now I can't get it to work.
This was my old code:
$query = "SELECT * FROM snippet_tools WHERE `db_title` LIKE ".sql_val('%'.$_GET['search'].'%')." OR `db_body`=".sql_val('%'.$_GET['search'].'%');
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
$anymatches = mysql_num_rows($result);
if ($anymatches == 0 ) {
I've upgraded my code to user oo
this is what I have:
$servername = "localhost"; $username = "xxxxxxxxx"; $password = "xxxxxxxxx"; $dbname = "xxxxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_search = "SELECT * FROM `questions` WHERE `q_title` LIKE ".sql_val('%'.$_GET['search'].'%')." OR `q_answered` LIKE ".sql_val('%'.$_GET['search'].'%');
$result = $conn->query($sql_search);
$anymatches = $result->num_rows;
if ($anymatches == 0 ) {
but every time I run it to perform a search I keep getting this error message:
Notice: Trying to get property of non-object in H:\root\site5\questions.php on line 611
You can refer W3 School site and get basic idea about the variations of MySQL and MySQLi and their functions. It is really helpful to go ahead with your project.
Ex Database connection example in both ways
So you can solve your problem based on that concept. Try to extract concepts.
Try this approach!
<?php
error_reporting(E_ALL);
$servername = "localhost"; $username = "root"; $password = "root"; $dbname = "cities";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$get = array('search' => "Lon");
$sql_search = "SELECT * FROM cities WHERE (city_name LIKE '%$get[search]%')";
$result = $conn->query($sql_search);
$anymatches = $result->num_rows;
if ($anymatches == 0 ){
echo "No matches!";
}
else
{
echo $anymatches . " match found!";
}
?>
Related
I have the following MySQL table and I would like to do that RVR11 (500) be equal with a php var $RVR11
I have this code but it doesnt work.
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$price = mysql_query("SELECT RVR11 FROM tablename");
$result = mysql_fetch_array($RVR11);
echo $result['RVR11'];
Sorry about this beginner question.
You should choose a database before any queries, you can pass the fourth arguments to new mysqli constructor.
After I add a column to my database, I want to retrieve it but not expected.
In PHP, I try reopening apache and mysql still not work.
Does anyone know how to resolve it? Thanks!
your question is not fully explanatory but with what I could try to understand you want to retrieve data or records from your database
you could try the code below and tweak it to work your way
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM databaseName";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data
while($row = $result->fetch_assoc()) {
print $row"<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Trying to read the status of a user in my mysql database from php
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Find status for row with Username of the Url?username=
$sql = "SELECT Status FROM Users WHERE Username = '".$_GET["username"]."'";
$result = mysql_query($sql) or die('Error connecting to database');
$username = mysql_result($result, 0, "Status");
echo 'Username Status is ' . $Status;
mysqli_close($conn);
?>
In result I'm getting is this:
Fatal error: Uncaught Error: Call to undefined function mysql_query() in /storage/ssd4/269/2113269/public_html/teststat.php:11 Stack trace: #0 {main} thrown in /storage/ssd4/269/2113269/public_html/teststat.php on line 11
Your syntax for mysqli is not correct, because mysqli is different from mysql syntax.
MySQLi stands for MySQL improved. It's an object-oriented interface to
the MySQL bindings which makes things easier to use.
I suggest you to use the mysqli version and don't mixup mysql syntax with mysqli syntax.
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Status FROM Users WHERE Username = '".$_GET["username"]."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row or remove while loop if you wish
while($row = $result->fetch_assoc()) {
echo 'Username Status is ' . $row['Status'] ;
}
} else {
echo "0 results";
}
$conn->close();
You mixed up mysql and mysqli
$result = mysql_query($sql) or die('Error connecting to database');
$username = mysql_result($result, 0, "Status");
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";
$tablename = "mapcoords";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
echo "Failure";
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo "ok";
}
$conn->close();
?>
Here is the code. So like I said, it can connect successfully, but the code won't successfully query. What's weird is that if I copy and paste the same code, which seems to be EXACTLY the same, it works. It makes no sense. I can't find a single difference between their code and my code besides the way they space things and the way I space things. Here is their code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["lat"]. " " . $row["lng"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem is that this query:
SELECT (lat, lng) FROM mapcoords
returns the folloowing error:
[21000][1241] Operand should contain 1 column(s)
You have to change the query to
SELECT lat, lng FROM mapcoords
I've been a few days working on my project, but just stay stuck a specific part.
My problem is that i like to get a value from the database (mysqli)
But i always receive a 0. So, no value.
This is my code:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user_idSQL = "SELECT user_id FROM ss_devices WHERE serialkey = '$serialkey'";
$user_idResult = $conn->query($user_idSQL);
$user_id = $user_idResult;
The script should see what the userid, it is in equality with the specified serial number.
But when i take a serial number like: FJRI433. I always get a 0. But in the database has this serial number user_id: 3.
I hope someone can help me out whith this problem.
Thanks.
You are executing the query but not fetching any results.
The manual has plenty of examples of using mysqli :
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
//Do stuff with the next row.
}
}
Try this perhaps...
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn=mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ( mysqli_connect_errno() ) {
die("Connection failed: " . mysqli_connect_error() );
}
$user_idSQL = "SELECT `user_id` FROM `ss_devices` WHERE `serialkey` = '$serialkey'";
if( $user_idResult = mysqli_query( $conn, $user_idSQL ) ) {
while ($row = mysqli_fetch_assoc($user_idResult)) {
echo $row['user_id'].'<br />';
}
}
mysqli_close( $conn );