How to work with MySQLi? - php

I am new on this MySQLi connection. I have system codes here and working on WAMP server localhost. I changed the old mysql connection to new mysqli connection and got rid of these "deprecated errors". But But my problem is that I could not login or access query (sql related function). What I am saying is that I got rid of deprecated errors but could not login/access my database.
//This is my OLD connection:
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("towertec_master") or die(mysql_error());
//This is my NEW connection
$link = mysqli_connect ("localhost", "root", "", "towertec_master") or die(mysql_error());
Also here is my OLD fetch connection:
$sql=mysql_query("SELECT organization_name,address,phone,email FROM system_config");
$row=mysql_fetch_assoc($sql);
$organization_name=stripslashes(trim($row['organization_name']));
$address=stripslashes(trim($row['address']));
$phone=stripslashes(trim($row['phone']));
$email=stripslashes(trim($row['email']));
NEW fetch connection:
$query = "SELECT organization_name, address, phone, email FROM system_config" or die("Error in the consult.." . mysqli_error($link));
$result = mysqli_query($link, $query);

As pointed out, your are still using the deprecated error function. Also, for your connection, you need to create the connection object which you do with "new".
$link = new mysqli_connect ("localhost", "root", "", "towertec_master");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
For your query, you are putting the error function in the wrong place.
$query = "SELECT organization_name, address, phone, email FROM system_config";
$result = mysqli_query($link, $query);
if (!$result) {
printf("Error: %s\n", mysqli_error($link));
}

Related

linking mit inventor2 to sql database error 1109

I have been trying to connect my hosted sql database with mitinventor2 in a registeration form and I am getting an error that says something about
URL. error 1109
This is my php code :
<?php
DEFINE ('DBUSER', '******');
DEFINE ('DBPW', '*****');
DEFINE ('DBHOST', 's******');
DEFINE ('DBNAME', '*******');
$dbc = mysqli_connect(DBHOST,DBUSER,DBPW);
if (!$dbc) {
die("Database connection failed: " . mysqli_error($dbc));
exit();
}
$dbs = mysqli_select_db($dbc, DBNAME);
if (!$dbs) {
die("Database selection failed: " . mysqli_error($dbc));
exit();
}
$clientusername = mysqli_real_escape_string($dbc, $_GET['clientusername']);
$clientemail = mysqli_real_escape_string($dbc,$_GET['clientemail']);
$clientpno = mysqli_real_escape_string($dbc,$_GET['clientpno']);
$clientpass = mysqli_real_escape_string($dbc,$_GET['clientpass']);
$query = "INSERT INTO ssudb(clientusername, clientemail, clientpno, clientpass) VALUES ('$clientusername', '$clientemail', '$clientpno', '$clientpass')";
$result = mysqli_query($dbc, $query) or trigger_error("Query MySQL Error: " . mysqli_error($dbc));
mysqli_close($dbc);
?>
I am sure about the database info yet I hide them for security reasons. My database is well structured with phpmyadmin.
The link to the php file is right with no errors, and here is my mit inventor block:
I hope I didn't validate any rule.
Thanks in advance!
note:
when open phpmyadmin somtimes i see the table is having new attributes with no values in it but the increment ID.
$clientlocation is missing in the VALUES

mysql_select_db failure

I am using phpMyAdmin + MySQL.
I created a database and am now trying to make the connection in a PHP script. The curious thing is that connecting to the DB works, so I get the "Connected to MySQL server" message, but I when it comes to selecting the 'petfood' database, the script shows "DIED at selection".
Any idea why? Thanks, and here's my piece of code:
<?php
$user = 'localhost';
$pass = 'password';
$db_name = 'petfood';
$db_conn = new mysqli("localhost", $user, $pass, $db_name) or die("Cannot connect to DB");
echo "Connected to MySQL server";
mysql_select_db($db_name) or die("DIED at selection");
echo "Database Selected";
?>
Spot the difference:
$db_conn = new mysqli("localhost", $user, $pass, $db_name) or die("Cannot connect to DB");
^----
mysql_select_db($db_name) or die("DIED at selection");
^---
If you had proper debugging, you'd have been told about the problem:
mysql_select_db($db_name) or die(mysql_error());
^^^^^^^^^^^^^^
Never output a fixed (useless) error message when you can have the system TELL you what's wrong.
1: using mysql
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
mysql_select_db("examples",$dbhandle) or die("Could not select examples");
$query = "SELECT name FROM mytable" ;
$result = mysqli_query($query);
2: using mysqli
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
$query = "SELECT name FROM mytable" ;
$result = mysqli_query($link, $query);

SQL cannot execute on live server

I am trying to exectute the following on the live server but it does not execute. On the local host everything is fine. I am able to execute other sql on the live server tho
$mysqli = new mysqli("localhost", "username", "password", "database");
if(isset($_POST['username'])&& isset($_POST['password']) ){
$username =$_POST['username'];
$password = md5($_POST['password']);
if ($mysqli) {
/* Prepared statement, stage 1: prepare */
if (!($stmt = $mysqli->prepare("select acc.accountId, acc.firstname, prof.imagename from accounts acc inner join profilepicture prof on prof.accountId=acc.accountId where acc.emailAddress=(?) and acc.password=(?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
/* Prepared statement, stage 2: bind and execute */
$stmt->bind_param("ss", $username,$password);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if(!empty($row)){
`enter code here`
`enter code here`// STATEMENTS TO EXECUTE IF DETAILS EXISTS
}
This could be the reason: You are logging into mysql with no default database, no username and no password
$mysqli = new mysqli("localhost", "", "", "");
should be something like this:
$mysqli = new mysqli("localhost", "user", "password", "database");
Read this http://php.net/manual/en/mysqli.quickstart.connections.php
In the live server or cpanel.. go to mysql section create the database and user and password... and give that user the permission to edit update and delete.. then in the
$mysqli = new mysqli("localhost", "", "", "");
add all the three the username password and database name..then.. it will work fine..
Hope it works

Php Mysqli Undefined Function

When executing the
mysqli_bind_result();
I receive an error saying "Call to undefined function".
My code looks as follows:
$mysqli = new mysqli("localhost", "root", "", "*****");
if(isset($_POST['login'])){
if($_POST['username']){
if($_POST['password']){
$username = $_POST['username'];
$passwordtmp = $_POST['password'];
$password = md5(md5($passwordtmp));
//Connect to Database//
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//Check if user exist in database//
$query = mysqli_query($mysqli,"SELECT * FROM users WHERE username = '$username'");
$numrows = mysqli_num_rows($query);
if($numrows == 1){
if($stmt = mysqli_prepare($mysqli, "SELECT password FROM users WHERE username = '$username'")){
mysqli_execute($stmt);
mysqli_bind_result($stmt,$passw);
while(mysqli_stmt_fetch($stmt)){
$pass = $passw;
}
Additional Information:
I transfered this code from a different computer to my laptop using the same connection to my mysql database.
$mysqli = new mysqli("localhost", "root", "", "*****");
I copied the exact database to my laptop and both my computer and laptop are using wamp.
I am wondering if this could be a connection to the database issue, or just a coding error. But it works perfectly on my original computer without any problems.
Thank you in advance. I will give credit for helpful answers.
You're probably using PHP > 5.4, as the function mysqli_bind_result was removed in PHP 5.4.
Change it to mysqli_stmt_bind_result and that should fix your problem.
mysqli_stmt_bind_result($stmt, $passw);

Cannot echo table from database

I try to echo my database (hosted on ipage.com) using php and I get this error : Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, cgiadmin#yourhostingaccount.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
What is wrong? $mysqli = new mysqli("site.ipagemysql.com", "user", "pass", "database");
alone, works, so it is not a error in the connection right? or I have to contact the host?
My code
<?php
$mysqli = new mysqli("site.ipagemysql.com", "user", "pass", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", $mysqli->host_info);
$result = mysqli_query($link, "SELECT DATABASE()"))
$row = mysqli_fetch_row($result);
printf("Default database is %s.\n", $row[0]);
mysqli_free_result($result);
?>
You have one ) too many on line 12, it's missing a semicolon and you haven't changed the $link variable to correspond with the $mysqli variable on line 3.
$result = mysqli_query($mysqli, "SELECT DATABASE()");
Also, you're mixing object oriented style with procedural style. I'd recommend you to use OO style only. So the code would be:
<?php
$mysqli = new mysqli("site.ipagemysql.com", "user", "pass", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", $mysqli->host_info);
$result = mysqli->query("SELECT DATABASE()"))
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]);
$result->close();
$mysqli->close();
?>

Categories