MySQL table value be equal with php var (then display it) - php

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.

Related

When I add a column, I can't retrieve it from mysql

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();
?>

converting from MySQL to MySQLi Object-oriented

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!";
}
?>

Mysqli to variable in PHP

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 );

I cant insert in mysql from php

whats the solution for this problem Number of elements in type definition string doesn't match number of bind variables
session_start();
$regValue1 = $_GET['un'];
$regValue2 = $_GET['pass'];
$regValue3 = $_GET['fn'] ;
$regValue4 = $_GET['ln'];
$regValue5 = $_GET['age'] ;
$regValue6 = $_GET['sex'];
$regValue7 = $_GET['em'] ;
echo "hello: ".$regValue3.".";
$servername = "localhost";
username = "root";
$password = "b4sonic";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("INSERT INTO register(un,pass,fn,ln,age,sex,email) VALUES (?,?,?,?,?,?,?)");
bind_param("sss",$regValue1,
$regValue2,$regValue3,$regValue4,$regValue5,$regValue6,$regValue7);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
I have this code in php , i am trying to insert data to mysql but i face this problem Number of elements in type definition string doesn't match number of bind variables
Your type string in your statement doesn't have enough type specifiers in it.
bind_param("sss",$regValue1,$regValue2,$regValue3,$regValue4,$regValue5,$regValue6,$regValue7);
says that you have type "sss" which only corresponds to 3 of the 7 variables you specified. You need to add types for the rest.
From the documentation:
var1
The number of variables and length of string types must match the parameters in the statement.
The commands you are using are for using PDO to connect to an sql database and you are using mysqli. I gave an example using mysqli below that should work. The other option would be to change the connection to PDO type rather than mysqli.
<?php
session_start();
$regValue1 = $_GET['un'];
$regValue2 = $_GET['pass'];
$regValue3 = $_GET['fn'] ;
$regValue4 = $_GET['ln'];
$regValue5 = $_GET['age'] ;
$regValue6 = $_GET['sex'];
$regValue7 = $_GET['em'] ;
echo "hello: ".$regValue3.".";
$servername = "localhost";
$username = "root";
$password = "b4sonic";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO register(un,pass,fn,ln,age,sex,email)".
"VALUES (`$regValue1`,`$regValue2`,`$regValue3`,`$regValue4`,`$regValue5`,`$regValue6`,`$regValue7`)";
$conn->query($sql);
echo $conn->affected_rows. " new records created successfully";
$conn->close();
?>

New mysqli () is reset to null when using a function

I am currently working with mySQL and php to create a web application.
In my main index.php file, I have
<?php
require_once('includes/config.php');
require_once('includes/functionName.php');
?>
My config.php is as below:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "DB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected Successfully!";
?>
When I call the actual function functionName("test");
it goes into the actual functionName.php and goes to
$sql = "UPDATE Table SET id = " . $test1 . " WHERE name = \"". $name . "\"";
echo $sql;
var_dump ($conn);
This returns UPDATE Table SET id = 09 WHERE name = "test" which is correct as an SQL query.
However, my $conn returns NULL, and as a result I get FATAL ERROR query on NULL.
Please tell me why my $conn is returning NULL, and failing my query?
Thank you it was solved after adding line global $conn

Categories