I'm having a trouble around starting a session. Here's my php:
<?php
session_start();
$con=mysqli_connect('localhost','root','','pttkhdt');
if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
else {
$a = $_SESSION['a'];
$knad ='SELECT * FROM admintb WHERE adID=' .$a;
$naad = mysqli_query($con,$knad);
$arad = array();
while($rowad=mysqli_fetch_assoc($naad)) $arad[] = $rowad;
}
?>
If I input that, when I tried to test run my site, it will show:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in...
But if I fix the line
$naad=mysqli_query($con,$knad);
Into:
$naad=mysqli_query($con,$knad) or die;
the page will "die" out and blank.
From the looks of it you're loading your mysqli_fetch_assoc into an array, using a while loop. However, a mysqli_fetch_assoc() is already loading an array.
Why don't you try this:
<?php
session_start();
$con=mysqli_connect('localhost','root','','pttkhdt');
if ( mysqli_errno($con) ) {
//Exit stops the rest of the script
exit( "Failed to connect to MySQL: " . mysqli_error($con) );
} else {
$a = $_SESSION['a'];
$knad = "SELECT * FROM admintb WHERE adID='" . $a . "'";
$arad = mysqli_fetch_assoc( mysqli_query($con,$knad) );
if (!$arad) { exit( mysqli_error($con) ); }
}
?>
The reason behind your sql error may be the fact that your prior SQL $knad statements says for example, adID=justatestvalue, so your SQL is searching for the column justatestvalue. Make sure to enclose actual statements in quotes.
mysqli_query($con,"SELECT * FROM user WHERE userName='$user'");
Check your error with mysql_error() and update your error here;
$naad=mysqli_query($con,$knad) or die(mysql_error());
or try this code below
$link=mysql_connect($host,$user,$pwd);
$db = mysql_select_db($dbname,$link);
if(!$db) die (mysql_error());
Related
I have been working on a website which has a xampp server and a database called users with a table called AccountDetails. About a year ago I got it to work perfectly, but the server I was using then required MySQL not MySQLi. Now I have to use MySQLi and can't even get the simplest of sql's SELECT function to work, any ideas would be much appreciated.
<?php
$link = mysqli_connect("localhost:3306", "root","", "users");
if(mysqli_connect_errno($link)){
echo "MySql Error: " . mysqli_connect_error();
} else {
echo"Connection Successful <br></br>";
}
echo("Check if still working <br></br>");
// -----------------------------//
echo("Its running <br></br>");
$result = $link->query("SELECT ID, UserName FROM AccountDetails");
return $result->result();
var_dump($result);
mysqli_close($link);
?>
The Query itself works when I plug it into the phpmyadmin SQL section and it returns the values that I expect it too.
I've spent days looking online for different answers but none of them work, and the var_dump only gives me "bool(false)" which I don't think I should be getting.
You can try this code
<?php
$link = mysqli_connect("localhost:3306", "root","", "users");
if(mysqli_connect_errno($link)){
echo "MySql Error: " . mysqli_connect_error();
} else {
echo"Connection Successful <br></br>";
}
echo("Check if still working <br></br>");
// -----------------------------//
echo("Its running <br></br>");
$sql_select = "SELECT * FROM AccountDetails";
$result = $link->query($sql_select);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "UserName: " . $row['UserName']. "<br>";
}
} else {
echo "No Records";
}
$link->close();
?>
I am doing a little bit of learning on mysql, php and the like. I'm using a shared hosting plan so am quite limited from a settings changes point of view.
I am attempting to run a simple mysql select command through PHP, but all i get back is a blank error
<?php
$typeID = $_GET['tid'];
//variables for the database server
$server = "localhost";
$user = "codingma_rbstock";
$pwd = "M#nL%V{%RI+h";
$db = "codingma_rbstock";
//variables for the database fields
$itemNo;
$itemNm;
$itemDesc;
$buyPr;
$sellPr;
$quan;
$dept;
//database connection
//create connection
$conn = new mysqli($server, $user, $pwd, $db);
//if the connection fails throw an error.
if ($conn->connect_error){
die("Connection Failed: " . $conn->connect_error);
}
echo "Welcome to " . $typeID . "<br>";
$sql = "select ITEM_NAME from stock where ITEM_NO='00001'";
if ($conn->query($sql) === TRUE){
$res = $conn->query($sql);
if ($res->num_rows > 0){
echo "success";
}
}else{
echo "Error: " .$sql . "<br>" . $conn->error;
}
echo $res;
?>
I have checked and it seems to be connecting to the database fine (I changed a few account details to see if that threw a different error and it did).
I am sure I am missing something completely obvious here! The below is the text output from the error;
Error: select ITEM_NAME from stock where ITEM_NO='00001'
Thanks for any help.
your problem is in this line
if ($conn->query($sql) === TRUE){
you are doing a variable type check ( === ), the result of that comparision will always fail because, for as long as you have data in your table and your query doesn't fail $conn->query($sql) will not return a boolean value
mysqli::query documentation says:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
You are using here a SELECT, therefore a successfull result won't be boolean
Try switching to
if ($conn->query($sql) == TRUE){
Or even better remove that if completely
EDIT
The better approach for that part of the code is:
$res = $conn->query($sql);
if ($res->num_rows > 0){
echo "success";
}
if ($res === false) {
echo "Error: " .$sql . "<br>" . $conn->error;
}
when I try this code I receive warning about second parameter to string. I have seen some answers in previous similar questions but I did not find the solution... As far as I can get the problem is with if statement?? Thanks.
if (isset($_GET['id'])) {
$str_id = $_GET['id'];
($conn->set_charset("utf8"));
if ($result=mysqli_query($conn, $q )) {
while ($obj=mysqli_fetch_object($result)) {
}
?>
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
while ($obj=mysqli_fetch_object($result))
{
printf("%s (%s)\n",$obj->Lastname,$obj->Age);
}
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
I want to check whether a specific entry is present in my database or not. If present then condition,if not then condition. I tried this code but got errors
<?php
$con=mysqli_connect("localhost","root","","student");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$classname = mysqli_real_escape_string($con, $_POST['class']);
$result = mysql_query("SELECT * FROM subjectinfo WHERE class = '{$classname}'", $con);
if(mysql_num_rows($result) == 0)
{
echo "No Such Entry In Table. Please ADD it First.";
}
else
{
echo "Entry Available";
}
}
mysqli_close($con);
?>
Errors :
Warning: mysql_query() expects parameter 2 to be resource, object given in C:\xampp\htdocs\pages\test.php on line 11
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\pages\test.php on line 13
No Such Entry In Table. Please ADD it First.
Like your comments. Make sure you don't mix up mysqli and mysql. mysql is deprecated so you're better off using mysqli.
$con=mysqli_connect("localhost","root","","student");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$classname = mysqli_real_escape_string($con, $_POST['class']);
$result = mysqli_query($con, "SELECT * FROM subjectinfo WHERE class = '{$classname}'");
if(mysqli_num_rows($result) == 0)
{
echo "No Such Entry In Table. Please ADD it First.";
}
else
{
echo "Entry Available";
}
}
mysqli_close($con);
?>
You are mixing MySQL APIs - mysql_ + mysqli_ they do not mix together. Plus, your DB connection's backwards in your query. The connection comes first.
Here:
<?php
$con=mysqli_connect("localhost","root","","student");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$classname = mysqli_real_escape_string($con, $_POST['class']);
$result = mysqli_query($con,"SELECT * FROM subjectinfo WHERE class = '{$classname}'");
if(mysqli_num_rows($result) == 0)
{
echo "No Such Entry In Table. Please ADD it First.";
}
else
{
echo "Entry Available";
}
}
mysqli_close($con);
?>
Also use or die(mysqli_error($con)) to mysqli_query()
Plus, add error reporting to the top of your file(s) which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Look into using prepared statements, or PDO with prepared statements, they're safer.
An insight:
Make sure your form element is indeed named.
I.e.:
<input type="text" name="class">
otherwise, you will receive an Undefined index class... warning.
Code :
<?php
$con=mysqli_connect("mysql17.000webhost.com","login","pwd","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$filter = mysqli_query($con,"SELECT * FROM bad_words");
$content = mysqli_fetch_array( $filter );
$old_word = $_POST("input");
$old_word = str_ireplace($content['word'], '[foul]', $old_word);
$filtered_word = $old_word;
Print($filtered_word);
mysqli_close($con);
?>
Error message :
Fatal error: Function name must be a string in /home/a7593238/public_html/bad_filter.php on line 13
Please help.
You are calling the superglobal array $_POST like a function. It is an array variable and you need to access them using the square brackets.
Like this.
$old_word = $_POST["input"];
Replace
$old_word = $_POST("input");
with
$old_word = $_POST["input"];