I'm trying to use a mysqli connection to retrieve rows from my database however I continue to receive a 500 internal server error no matter what I try.
$getUserQuery = "SELECT * FROM members WHERE name = ? AND member_id > 0";
$getUserStatement = $mysqli_conn->prepare($getUserQuery);
$getUserStatement->bind_param("s", $name);
$mysqli_conn->ping() results in a value of true so I know there's no issue with the database connection. var_dump($getUserStatement) results in bool(false) so there's some issue with the prepare. Whole code:
$user_dirty = $_GET['u'];
$pass_dirty = $_GET['p'];
$getUserQuery = "SELECT * FROM members WHERE name = ? AND member_id > 0";
$getUserStatement = $mysqli_conn->prepare($getUserQuery);
if ($getUserStatement) {
echo($getUserStatement);
} else {
echo("not good");
}
$getUserStatement->bind_param("s", $user_dirty);
$getUserStatement->execute();
$getUserResult = $getUserStatement->get_result();
And how I create my DB connection:
$mysql_host = "host";
$mysql_user = "user";
$mysql_password = "pass";
$mysql_database = "db";
$mysqli_conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);
You gets 500 internal error because statement is false probably(https://stackoverflow.com/a/845025/2962442 turn on display errors), not resource like it should be, it can be caused by lost connection, try ping or reconnect before prepare.
Example good code:
$getUserQuery = "SELECT * FROM members WHERE name = ? AND member_id > 0";
$getUserStatement = $mysqli_conn->prepare($getUserQuery);
if ($getUserStatement) {
$getUserStatement->bind_param("s", $name);
} else {
//statement is false, not good
}
and good example of connection part
$mysql_host = "host";
$mysql_user = "user";
$mysql_password = "pass";
$mysql_database = "db";
$mysqli_conn = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_database);
if (!$mysqli_conn) {
//not connected...
}
Related
I want to get some data from database with this code
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "test";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $bd);
$result = mysql_query("SELECT author_id FROM user");
while($data = mysql_fetch_array($result)) {
$sn=$data['author_id'];
}
$lastresult = mysql_query("SELECT * FROM post WHERE id='".$sn."'");
This code only work fetches one author_id
$sn=$data['author_id'] and $lastresult works only with one author_id and doesn't fetches all the author_id.
How can I get data for all author_id ?
You are overwritting $sn each loop.
$result = mysql_query("SELECT author_id FROM user");
$sn = [];
while($data = mysql_fetch_array($result)) {
$sn[] = $data['author_id'];
}
$sn = implode(',',$sn);
$lastresult = mysql_query("SELECT * FROM post WHERE id IN (".$sn.")");
PS: mysql_* functions are deprecated and has security issues. Consider replace with mysqli_* functions or PDO.
as you can see my code down below, the method I used to detect does mysql_select_db() return true or false. It does return true but I still ge the "No database selected" error.
$host = "localhost";
$sql_username = "root";
$sql_password = "password";
$sql_db = "tryckstore";
$con = mysqli_connect($host, $sql_username, $sql_password) or die("Error");
if (!mysqli_select_db($con, "tryckstore")) {
die("Error selecting databse.");
} else {
echo "ok";
}
It used to work actually, then I suddenly get this error. Thanks in advance.
Use this one:
$host = "localhost";
$sql_username = "root";
$sql_password = "password";
$sql_db = "tryckstore";
$con = mysqli_connect($host, $sql_username, $sql_password,$sql_db) or die("Error");
if (!mysqli_select_db($con, "tryckstore")) {
die("Error selecting databse.");
} else {
echo "ok";
}
//pass the $sql_db as your 4th argument in the mysqli_connect also
for simplier approach:
$host = "localhost";
$sql_username = "root";
$sql_password = "password";
$sql_db = "tryckstore";
$con = mysqli_connect($host, $sql_username, $sql_password,$sql_db) or die("Error");
Ok. Hello guys
I have some problems with my database connection and using it in php.
My normal file structure:
db.php
$mysql_server = "server";
$mysql_user = "user";
$mysql_password = "password";
$mysql_db = "name";
$db = new mysqli($mysql_server, $mysql_user, $mysql_password, $mysql_db);
if ($db->connect_errno) {
exit();
}
$db->set_charset("utf8");
functions.php file
require_once("db.php");
function func_name() {
global $db;
//doing my work here with $db;
}
I'm 100% sure the credentials i user are ok so this is not the problem.
Can you give me some advice regarding this? I used this structure for every project and now i'm losing my mind trying to figure it out. I bet is something that i missed!
please, help! Thank you!
try using below code !!!
<?php
$mysql_server = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_db = "dbName";
$db = new mysqli($mysql_server, $mysql_user, $mysql_password, $mysql_db);
if ($db->connect_errno)
{
exit();
}
$db->set_charset("utf8");
function func_name() {
global $db;
$sql = "SELECT text FROM tableName";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo $row["columnName"];
}
} else {
echo "0 results";
}
}
func_name();
mysqli_close($db);
?>
This is just a test case I wrote but my main question is my whole
application works locally connect to MySQL and return a couple thousand
records I turn error reporting on EALL for the page and I don't get any error all I actual get for output of this test case is Number Number Number Number Number five times which doesn't make any sense as I am grabbing one row
by primary key in this example. Any way I am new at PHP so any reference
for going from XAMPP to lamp would be appreciated. URL is http://prestigeworldwide.me/game/
<?php
$servername = "localhost";
$username = "root";
$password = "Mypass";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sqltest = sprintf("SELECT num, num2 FROM mytest WHERE num = 2");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$resulttest = mysqli_query($conn, $sqltest);
foreach($resulttest as $row){
echo("Number".$row['num'].$row['num2']);
}
mysqli_close($conn);;
?>
I suggest you to use PDO.
Take a look here.
$dbtype = "mysql";
$dbhost = "localhost";
$dbname = "*****";
$dbuser = "*****";
$dbpass = "*****";
try{
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname; charset=utf8", $dbuser,$dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
echo $e->getMessage();
}
$sql = "SELECT num, num2 FROM mytest WHERE num = '2'";
$q = $conn->query($sql);
while ($r = $q -> fetch()):
echo "Number" . $r['num'] . $r['num2']);
endwhile;
So, I'm writing this application in PHP where the user has a "Student's Name" and each user has a unique student name. So, before I go any further with my problem, here is the code
*Note I've already prevented the SQL injections
function hello($username123) {
// Connect to Database //
$host3 = "db";
$username3 = "db";
$password3 = "db";
$db3 = "db";
$con3 = mysqli_connect($host3,$username3,$password3,$db3) or die("Can not connect to Server.");
$query3 = mysqli_query($con3,"SELECT student1 FROM users WHERE username = '$username123'");
$student1name = "$query3";
return $student1name;
So, the person enters the username which the registered before hand and each user has a student name.I start a query which selects student1 and student1 is equal to student1name. Student 1 name is then defnied as query3. When I test it all out, all I get is (null).. Does anyone know the problem? Thank you!
I suspect what you want is something like this:
function hello($username123) {
// Connect to Database //
$host3 = "db";
$username3 = "db";
$password3 = "db";
$db3 = "db";
$con3 = mysqli_connect($host3,$username3,$password3,$db3) or die("Can not connect to Server.");
$query3 = mysqli_query($con3,"SELECT student1 FROM users WHERE username = '$username123'");
while ($row = mysqli_fetch_array($query3))
{
$student1name = $row['student1'];
}
return $student1name;
This will put the contents of the last returned row of your query, column "student1", into the variable $student1name, and return it.
You are not fetching data from result. Try this:
function hello($username123) {
// Connect to Database //
$host3 = "db";
$username3 = "db";
$password3 = "db";
$db3 = "db";
$con3 = mysqli_connect($host3,$username3,$password3,$db3)
if (!$con3)
throw new Exception("Connection error");
$result = mysqli_query($con3,"SELECT student1 FROM users WHERE username = '$username123'");
if ($result)
return $result->fetch_object();
else
throw new Exception("Query error: " . mysqli_error($con3));
}