How to use new format - php

I have the following query, but this one is old. There should be a new way of writing the following code. Can anyone tell me how i should write this:
$get_test = mysql_query("select test from test_table where id = '1'");
$test = mysql_result($get_test, 0);
Ik would like to write it in: MYSQLI instead of mysql.

Maybe this is what you are looking for:
Mysqli:
<?php
$strSQL = "select test from test_table where id = '1'";
$query = mysqli_query($con, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo $result["test"]."
";
}
?>
PDO:
<?php
$id = 1;
try {
#connection
$conn = new PDO('mysql:host=localhost;dbname=myDB', $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = $conn->prepare('SELECT test FROM test_table WHERE id = :id');
$data->execute(array('id' => $id));
while($rows = $data->fetch()) {
print_r($rows);
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}?>

You should use PDO:
$db = new PDO("...");
$statement = $db->prepare("select test from test_table where id = :id");
$statement->execute(array(':id' => "test"));
$row = $statement->fetch();

Related

PDOStatement::execute, How to execute?

How to execute this query?
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM attendancy
WHERE user_id = $user_id
AND date = '$date'";
$q = $pdo->prepare($sql);
To execute you need to use the execute function. This usage of the prepare function also is not safe, each variable should be a placeholder.
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM attendancy
WHERE user_id = ?
AND date = ?";
$q = $pdo->prepare($sql);
$q->execute(array($user_id, $date));
while($result = $q->fetch(PDO::FETCH_ASSOC)) {
print_r($result);
}

How do I reconnect my web pages on my website after updating to PHP 7 with a MySQL database 5.0.0?<?

I added the i updates to communicate with the database & now the page links don't work.
<?php
// Connect to database
$link=mysqli_connect('localhost', 'xxxxx', 'xxxxx');
mysqli_select_db($link, 'waddellc_PHRDB');
$sql = "SELECT * FROM quotes ORDER BY id";
$result = mysqli_query($link, $sql) or die(mysql_error());
$tenant_quotes = array();
$owner_quotes = array();
while($row = mysqli_fetch_array($result)) {
This should do the work, using PDO :
$servername = "localhost";
$username = "username";
$password = "password123";
$conn = null;
try {
$conn = new PDO("mysql:host=$servername;dbname=databaseName", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
if(!is_null($conn)){
$stmt = $conn->prepare("SELECT * FROM quotes ORDER BY id");
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
}
I also think you need to update your database, it's quite old now.

this is not return user_id in php with MySQL

try{
$query = "SELECT user_id FROM user WHERE username = '".$username."'";
$result = $dbhandle->query($query,PDO::FETCH_OBJ);
//$stmt->bindParam(":username",$username);
// $result = $stmt->fetchAll(PDO::FETCH_BOTH);
while($row = $result->fetch()){
$id = $row['user_id'];
$_SESSION['user_id'] = $id;
}
}catch(PDOException $e){ echo $e->getMessage();}
You can use this code as sample code
$DBH = new PDO( "connection string goes here" );
$STH - $DBH -> prepare( "SELECT user_id FROM user WHERE username = '".$username."'" );
$STH -> execute();
$result = $STH -> fetch();
echo $result ["user_id"];
$DBH = null;
Hope this helps.

mysqli to PDO Translation

The following code is for me at least straight forward. I want to to achieve the same thing using PDO. However, try as I may I simply can't get my head around the concept. Could someone please explain?
//Connect to a database.
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die("Couldn't connect to database.");
//Delete from the multiple tables.
$sql = "DELETE FROM table1, table2, tables3, tables4 WHERE id='75'";
$result = mysqli_query($link , $sql);
Here you go, using prepared statements just for showing as id = 75 is no user input - but that's the better way and using a transaction - in case you want to delete/update/insert more data at a time this is way faster.
$id = 75;
try {
$pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME , DB_USER, DB_PASS);
$pdo->beginTransaction();
$st = $pdo->prepare('DELETE FROM table1 WHERE id = :id');
$st->execute(array(':id', $id));
$st = $pdo->prepare('DELETE FROM table2 WHERE id = :id');
$st->execute(array(':id', $id));
$st = $pdo->prepare('DELETE FROM table3 WHERE id = :id');
$st->execute(array(':id', $id));
$st = $pdo->prepare('DELETE FROM table4 WHERE id = :id');
$st->execute(array(':id', $id));
$pdo->commit();
}
catch (PDOException $e) {
die('Error!: ' . $e->getMessage() . '<br/>');
}
SIDENOTE:
To write less, do it like this:
$array = array('table1','table2','table3','table4');
foreach ($array as $table) {
$st = $pdo->prepare('DELETE FROM '.$table.' WHERE id = :id');
$st->execute(array(':id', $id));
}
You can't do multiple table delete in a single query Try with foreach in PDO
$pdo = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$tables = array("table1","table2","table3","table4");
foreach($tables as $table) {
$sql = "DELETE FROM $table WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $id); // $id or '75'
$stmt->execute();
}

PHP retrieve data from table row and store to variable

i guess the questions says it all. The result of my query results into a row which will match the condition. I want to get each data from each table column and put into a variable.
$getinfo = "select
user_firstname,
user_middlename,
user_lastname
from tempuserinfo
where user_email ='$ead'
and activation_code='$eac'";
$query = mysql_query($getinfo, $db);
if(!$query) {
echo'something went wrong.';
} else {
//put them into variables
$firstname = mysql_fetch_object($query, 'user_firstname');
$middlename = mysql_fetch_object($query, 'user_middlename');
$lastname = mysql_fetch_object($query, 'user_lastname');
}
If you are getting multiple results, you can loop through them like this:
$getinfo = "select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email ='$ead' and activation_code='$eac'";
$query = mysql_query($getinfo, $db);
while ($row = mysql_fetch_array($query)) {
$firstname = $row['user_firstname'];
$lastname = $row['user_lastname'];
}
If you are only getting one row back (make sure you add a LIMIT to your SQL statement), then you can use something like this:
$getinfo = "select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email ='$ead' and activation_code='$eac'";
$query = mysql_query($getinfo, $db);
$row = mysql_fetch_array($query);
$firstname = $row['user_firstname'];
$lastname = $row['user_lastname'];
mysql_* function are deprecated. In documentation its recommended like in latest versions of PHP they are going to use mysqli_* or PDO.
Below are the script using mysqli for your question:
$mysqli = new mysqli("localhost", "root_user", "root_password", "database");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$getinfo = "select
user_firstname,
user_middlename,
user_lastname
from tempuserinfo
where user_email ='$ead'
and activation_code='$eac'";
if ($result = $mysqli->query($getinfo)) {
while ($row = $result->fetch_object()) {
$firstname = $row->user_firstname;
$middlename = $row->user_middlename;
$lastname =$row->user_lastname;
}
$result->close();
}
else
{
echo'something went wrong.';
}
an example with mysqli, mysql as this deprecated
$sql = "select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email ='$ead' and activation_code='$eac'";
$query = mysqli_query($conn, $sql);
while($rs = mysqli_fetch_assoc($query)){
$user_firstname = $rs['user_firstname'];
$user_middlename = $rs['user_middlename'];
$user_lastname = $rs['user_lastname'];
}
if you want to have all fields in order to consult them later, add them to an array
$i=0;
while($rs = mysqli_fetch_assoc($query)){
$data[$i] = array('user_firstname'=>$rs['user_firstname'], 'user_middlename'=>$rs['user_middlename'], 'user_lastname'=> $rs['user_lastname']);
$i++;
}
print_r($data);
This should work:
$result = mysql_fetch_object($query);
$firstname = $result->user_firstname;
$middlename = $result->user_middlename;
$lastname = $result->user_lastname;
BUT:
Use MySQLi or PDO sintead mysql module is deprecated already
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
try {
$sth = $dbh->prepare("select user_firstname, user_middlename, user_lastname from tempuserinfo where user_email = ? and activation_code=?");
$sth->execute(array($ead,$eac));
$row = $sth->fetch(PDO::FETCH_BOTH);
$user_firstname = $row['user_firstname'];
$user_middlename = $row['user_middlename'];
$user_lastname = $row['user_lastname'];
} catch (PDOException $e) {
echo 'Database operation failed: ' . $e->getMessage();
}
Really, do learn to use PDO, because it is as easy as using mysql_*, but much safer and better.

Categories