Php seven update messed up my program - php

So recently with the php 7 update they removed all the mysql commands. however one of my programs for my internship was using such commands in one of the pages. however when i change it to mysqli it no longer works like it should. can someone help perhaps?
$connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());
//select MySQLi dabatase table
$db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());
$result = mysqli_query($connection, "SELECT * FROM gebiedsmanagers WHERE Datum >= NOW()");
Pastebin code
With kind regards,
Dayne Tersluijsen

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given:
mysqli_select_db("prodyne", $con);
To
mysqli_select_db($con, "prodyne")
Try this out!
<?php
//MySQLi information
$db_host = "localhost";
$db_username = "username";
$db_password = "password";
//connect to mysqli database (Host/Username/Password)
$connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());
//select MySQLi dabatase table
$db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());
//fetch information from your database
$result = mysqli_query($connection, "SELECT * FROM gebiedsmanagers WHERE Datum >= NOW()");
while($row = mysqli_fetch_array($result))
{
$counter ++;
?>
<tr><td><?php echo date('d-m-Y', strtotime($row['Datum']));?></td><td>
<?php echo $row['Voor1500']; ?></td><td><?php echo $row['Na1500']; ?>
</td></tr>
<?php
if($counter >= 120) {
break;
}
I hope this has helped you.

Related

Need help to display data from mysql database

I would need some help with showing data that I have on my database but I can't seen to be able to.`
$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";
$connect = mysqli_connect($servername, $username, $password, $dbname) or die ("connection failed");
//Query
$query = "SELECT * FROM 'Students'";
mysqli_master_query($dbname, $query) or die ("Error while Query");
$result = mysqli_master_query($dbname, $query);
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
echo "<p>".$row['Name']."</p>";
};
mysql_close($connect);
?>`
I am pretty new to this so I could have missed something simple. Any help appreciated.
Below is a sample code of the normal procedure to connect to a database and to select data from it. Please follow this type of coding since MySQL is now deprecated and MySQLi is used.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
For further reference check out http://php.net/manual/en/book.mysqli.php and also https://www.w3schools.com/php/php_mysql_insert.asp

Cant connect php to mysql

New to php and am connecting form attributes to php to connect to a godaddy mysql. Every attempt ends in a blank screen with no error messages. Is there any syntax errors the jump out? My sublime text wont register php syntax, but thats another problem for another time. I may need to call up godaddy support? the password has been removed for privacy.
<?php
$servername = "localhost";
$dbusername = "jaysenhenderson";
$dbpassword = "xxxxx";
$dbname = "EOTDSurvey";
$con = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
mysql_select_db('EOTDSurvey', $con)
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo("Connected successfully");
$_POST['BI1']
$_POST['BI2']
$_POST['BI3']
$_POST['BI4']
$_POST['BI5']
$_POST['BI6']
$_POST['BI7']
$_POST['BI8']
$_POST['BI9']
$_POST['BI10']
$_POST['BI11']
$_POST['BI12']
$_POST['BI13']
$_POST['BI14']
$_POST['BI15']
$sql = "INSERT INTO Survey1(BI1)"
$sql = "INSERT INTO Survey1(BI2)"
$sql = "INSERT INTO Survey1(BI3)"
$sql = "INSERT INTO Survey1(BI4)"
$sql = "INSERT INTO Survey1(BI5)"
$sql = "INSERT INTO Survey1(BI6)"
$sql = "INSERT INTO Survey1(BI7)"
$sql = "INSERT INTO Survey1(BI8)"
$sql = "INSERT INTO Survey1(BI9)"
$sql = "INSERT INTO Survey1(BI10)"
$sql = "INSERT INTO Survey1(BI11)"
$sql = "INSERT INTO Survey1(BI12)"
$sql = "INSERT INTO Survey1(BI13)"
$sql = "INSERT INTO Survey1(BI14)"
$sql = "INSERT INTO Survey1(BI15)"
if ($conn->query<$sql) === TRUE) {
echo "IT FUCKING WORKS.";
}
else{
echo "didnt workkkkkk";
}
$conn->close();
?>
please connect database like this...
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}
// 2. Select a database to use
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
die("Database selection failed: " . mysqli_error());
}
And Use mysqli_select_db instead of mysql_select_db
And insert semi-colon (;) after every line end according to php code standard.
There are a lot of issues with this code, as mentioned the mysqli_select_db issue. The $_POST['BIx'] will also cause errors because there is no semi-colon after each statement. You're missing a '(' on the line if ($conn->query<$sql) === TRUE) { not to mention that line will not work anyway because you're logically comparing a resource type (I think) to a string.
You're also never executing the insert statements. All around I seriously think you should practice PHP coding some more and read up on how to use mysqli properly: see here.
Regards
EDIT: You also have a closing PHP tag at the end of your script which is generally not a good idea as explained here
EDIT 2: Also using an IDE such as Netbeans is always a good idea as it can highlight syntax errors instead of asking SO to do it for you ;)
<?php
$servername = "localhost";
$dbusername = "jaysenhenderson";
$dbpassword = "xxxxx";
$dbname = "EOTDSurvey";
$con = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
mysqli_select_db('EOTDSurvey', $con);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo("Connected successfully");
############# Function For Insert ##############
function insert($tableName='',$data=array())
{
$query = "INSERT INTO `$tableName` SET";
$subQuery = '';
foreach ($data as $columnName => $colValue) {
$subQuery .= " `$columnName`='$colValue',";
}
$subQuery = rtrim($subQuery,', ');
$query .= $subQuery;
pr($query);
mysqli_query($con,$query) or die(mysqli_error());
return mysqli_insert_id();
}//end insert
#########################################
if(isset($_POST['submit'])){
unset($_POST['submit']);
//print_r($_POST);
$result=insert('Survey1',$_POST);
if($result){
echo '<script>window.alert("Success!");</script>';
echo "<script>window.location.href = 'yourpage.php'</script>";
}
}
$conn->close();
?>

Displaying an image where image path stored in Database using Id,

I am trying display images on webpage, where image path stored in database and images is stored in server.But i am not able to display those images using following codes, so pls somebody help me with this issue,..
<form method="post" enctype="multipart/form-data" action="file_upload.php">
<table>
<?php
$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name")or die("cannot select DB");
$path1 = mysql_query("select * from '$tbl_name' where id='1'");
$path2 = mysql_query("select * from '$tbl_name' where id='2'");
$path3 = mysql_query("select * from '$tbl_name' where id='3'");
echo '<tr><td><img src="$path1"></td>' ;
echo '<td><img src="$path2"></td>' ;
echo '<td><img src="$path3"></td></tr>' ;
?>
</table>
</form>
A couple of things before we begin:
I recommend using mysqli, and will do so in my examples below (mysql is deprecated)
I'll use a cycle to iterate through results, instead of querying each element individually.
PHP code
$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if (!$conn)
{
die('Could not connect: ' . mysqli_connect_error());
}
$result = mysqli_query($con, "SELECT * FROM `$tbl_name`");
while ($row = mysqli_fetch_array($result))
{
echo '<tr><td><img src="'.$row['image'].'"></td>' ;
}
Note how I first "fetched" the results from the query. The query first returns a mysqli object, one that contains all the results the query returned. These have to be extracted; the method I present is widely used in examples elsewhere as well.
Also note how the backtick character was used instead of single quotes when referring to the table.
After execute the query we will get the result set cursor. We need to iterate it to get all the rows .
Try the below code it should work.
$result = mysql_query("SELECT * FROM '$tbl_name' WHERE id IN ( 1, 2, 3 ) ");
if (!$result) {
// show your respective error messages
}else{
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td><img src="'.$row['database_column_name'].'"></td>' ;
}
}
mysql_query(); has 2 arguments.
argument1: the connection.
argument2: the query.
this is what i will do if i was you:
$sql = "select * from `$tbl_name` where `id` between 1 and 3";
$path = mysql_query($conn, $sql);
while($row = mysqli_fetch_array($patht)) {
echo '<tr><td><img src="' . $row['name of colum'] . '"></td></tr>' ;
}
mysql_close($con);
sorry for my bad english. i'm Dutch.

Converting my deprecated mysql to mysqli

I'm a beginner here I need to learn the basic in converting from mysql to mysqli. I want to start in configuration first. I have used this connect.php and it's working now I wanted to switch to undeprecated mysqli. Please advise me how to modify this script.
<?php
$host = "localhost";
$dbusername = "root";
$dbpassword = "nopassword";
$dbname = "student";
$link_id = mysql_connect($host, $dbusername, $dbpassword);
if(!$link_id){
die(mysql_error("Can`t Connect To database"));
}
else{
$db = mysql_select_db($dbname, $link_id);
}
if(!$db){
die(mysql_error("Can`t select database"));
}
return;
?>
According to my comment....
you could do it that way
$host = "localhost";
$dbusername = "root";
$dbpassword = "nopassword";
$dbname = "student";
$link = mysqli_connect($host,$dbusername,$dbpassword,$dbname) or die("Error " . mysqli_error($link));
$query = "SELECT knowhow FROM google WHERE myknowhow='leaks'";
$knowhow = mysqli_query($link, $query);
this is just the simplest way you can do it.
But you should use it like this (OOP)
$db = new mysqli($host,$dbusername,$dbpassword,$dbname);
$knowhow = $db->query("SELECT knowhow FROM google WHERE myknowhow='leaks'");
According to your comment:
You dont need the IF/ELSE statements anymore. All errors are catched with
die("Error " . mysqli_error($link));
In the oop, errors are catched in
... new mysqli($host,$dbusername,$dbpassword,$dbname);
EDIT
Since you where blocked asking new questions ill just update this answere:
$link = mysqli_connect($host,$dbusername,$dbpassword,$dbname) or die("Error " . mysqli_error($link));
$query = "SELECT * FROM student_information where student_id='{$_SESSION['user_id']}'";
$knowhow = mysqli_query($link, $query);
$data = mysqli_fetch_array($result);
$numRows = mysqli_num_rows($result);
$i = 0;
while($i < $numRows){
echo $data[$i++] . "<br />";
}
// I would not use a while in this case. I would prefere a foreach
// just to prevent from using a endless loop and 1 row less code :)
foreach($data as $d){
echo $d . "<br />";
}
Please use MySqli as a Class like $db = new Mysqli();

Error occurred ... no database selected

I'm trying to connect to a database, check a column for whether a value exists or not, then execute a function based on whether or not that value exists.
Here's my code.
$con = mysql_connect('localhost','root','','users');
$sql = "SELECT * FROM allUsers WHERE username = '".mysql_real_escape_string($_POST["username"]) . "'";
$result = mysql_query($sql,$con) or die("Error occurred in [$sql]: " . mysql_error());
$count = mysql_num_rows($result);
if ($count != 0){
echo "Username is already taken";
echo "$count";
mysql_close($con);
}
else{
createUser($_POST["name"],$_POST["username"],$_POST["password"],$_POST["email"]);
}
The thrown error is:
Error occurred in [SELECT * FROM allUsers WHERE username = 'Admin']: No database selected.
I'm almost entirely sure that it comes from the $result line, but haven't a clue as to why.
I feel like this is a simple solution, and I'm just missing something minor.
I'm very new to MySQL (today is my first day, actually), so please keep solutions as simple as possible.
You forgot to call mysql_select_db after connecting:
$con = mysql_connect('localhost','root','');
mysql_select_db('users', $con);
Unlike MySQLi or PDO, mysql_* libraries does not take database as argument on the connection string, however if you were to migrate to either MySQLi or PDO.
MySQLi:
$con = new mysqli('localhost', 'root', '', 'users');
PDO:
$con = new PDO('mysql:host=localhost;dbname=users', 'root', '');
In MySQLi your code would look like this:
<?php
$host = "localhost";
$user = "root";
$pass = "";
$database = "users";
$con = mysqli_connect($host,$user,$pass,$database);
if($con->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
$stmt = $con->prepare("SELECT * FROM allUsers WHERE username = ? LIMIT 1");
$stmt->bind_param('s',$_POST["username"]);
if (!$stmt->execute())
die('Failed to excute with error ' . $con->error);
$stmt->store_result();
$count = $stmt->num_rows;
$stmt->close();
if ($count > 0)
{
echo "Username is already taken.";
}
else
{
createUser($_POST["name"],$_POST["username"],$_POST["password"],$_POST["email"]);
}
I think your error is quite obvious, you need to specify the database you want.
mysql_select_db("databaseName", $con);
With that taken care of, please, please don't use mysql_ libraries the are vulnerable to SQL injection and will soon be removed.

Categories