i am working on jquery data table with help of jquery plug in. I am fetching data from data base. But i don't know why my sql query is not valid, but query is totally correct.
<?php
$conn = mysqli_connect("localhost", "root","","data_table");
$query = "select * from data_table";
$result = mysqli_query($conn, $query);
?>
please put your database name
$conn = mysqli_connect("localhost", "root","","YourDatabaseName");
please put your database table name
$query = "select * from DatabaseTableName";
Related
I'm not sure my question makes much sense so I will try my best to explain. Basically, I want the $_GET['quote_id'] to be a condition in the query but it is displaying on the web page instead, I'll post a picture as an example.
Code.php
<?php
$connect = mysqli_connect("localhost", "root", "", "quote");
$query = "select * from `quote` where quote_id = '". $id = print_r($_GET['quote_id'])."'";
$result = mysqli_query($connect, $query);
?>
As you can see, it is displaying on the web page instead of being a part of the query which is my goal
Thanks for your help,
I dont know why you used print_r (a function that prints human-readable information about a variable), this is why the output is showing on your page.
Try change to this, first dont attribute and use a variable at same, do only one thing at time for readability, second use filter_input to sanitize your inputs or prepared statements to avoid SQL Injection attacks:
<?php
$id = filter_input($_GET['quote_id'], FILTER_SANITIZE_NUMBER_INT);
$connect = mysqli_connect("localhost", "root", "", "quote");
$query = "select * from `quote` where quote_id = '". $id ."'";
$result = mysqli_query($connect, $query);
?>
I want to import text file to table in mySQL and show in php
$connection = mysqli_connect("127.0.0.1", "root", "", "schedule");
if($connection){
$query1 = LOAD DATA LOCAL INFILE "practice.txt" INTO TABLE schedule.assigned_classes;
$query = "SELECT * FROM schedule.assigned_classes";
}
but it doesn't work. May you help me?
In order to execute sql, you must call mysqli_query() function
$connection = mysqli_connect("127.0.0.1", "root", "", "schedule");
if($connection){
$query1 = LOAD DATA LOCAL INFILE "practice.txt" INTO TABLE schedule.assigned_classes;
mysqli_query($con,$query1);
$query = "SELECT * FROM schedule.assigned_classes";
$result = mysqli_query($con,$query);
...
}
mysqli_close($con);
I am attempting to post a column into my database here as a test and I am unable to do so. I've used the code below and it doesn't seem to be posting. Unless I am missing a trick with PHPmyAdmin I cannot seem to get it working. Any chance anyone could help? Thanks in advance!
<?php
$link = mysqli_connect("XXXX", "XXXX",
"XXXX", "XXXX");
if (mysqli_connect_error ()) {
die("The connection has failed");
}
$query = "INSERT INTO `users` (`email`, `password`)
VALUES('owen#owen.com', 'hfudhf8ahdfufh')";
mysqli_query($link, $query);
$query = "SELECT * FROM users";
if($result = mysqli_query($link, $query)) {
$row = mysqli_fetch_array($result);
echo"Your Email is ".$row["email"];
echo" and your Password is ".$row["password"];
}
?>
The problem is that you're only fetching one row of results. Unless the table was empty before you ran the script, there's no reason to expect that row to be the one that you just added.
If the table has an auto-increment ID field, you can fetch that row:
$query = "SELECT * FROM users WHERE id = LAST_INSERT_ID()";
I have looked for an answer for ages now, lots of similar questions but found no solutions yet...
Anyway,
all I am trying to do is get the id of a user from the database using a mysqli_query, the query seems to work when I use it in phpmyadmin but doesn't when I use it as part of a php script.
$username = "bob";
$db = mysqli_connect("localhost", "username", "password", "user_data");
$sql1 = "select id from user_information where username='$username'";
$id = mysqli_query($db, $sql1) or die(mysql_error());
echo $id;
The database connection works fine, I am able to input data through php.
Any suggestions? (Anyone's help is greatly appreciated).
you can't print the result from mysqli_query, it is mysqli_resource and for dumping the error you need to change mysql_error() to mysqli_error()
$username = "bob";
$db = mysqli_connect("localhost", "username", "password", "user_data");
$sql1 = "select id from user_information where username='$username'";
$result = mysqli_query($db, $sql1) or die(mysqli_error());
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo $row['id'].'<br>';
}
So I am trying to download JSON into an iOS application and I don't really know a lot about PHP.
Currently I'm using a generic php scrip as a delegate that connects the the MySQL database and returns JSON results.
<?php
$host = "localhost"; //database host server
$db = "***"; //database name
$user = "root"; //database user
$pass = "root"; //password
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db("***", $connection);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM table";
$resultset = mysql_query($query, $connection);
$records = array();
//Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>
The issue with this is that I don't want to encode the entire table in JSON I just want to return a few select data fields from the particular table so I don't download unnecessary data.
I know that you do this in the SELECT query but something is wrong with my syntax for this.
I had been trying with:
$query = "SELECT *[datafield],[otherdatafield],... FROM table";
but that doesnt seem to be working.
Also, In that table there are two separate data fields that are used to build a URL for an image, and Im not sure how to combine the two here so that they are returned in the JSON as one field.
For example: I Have a base string
"http://wwww.mysite.com/"
and i would like the add the two data Fields to that string so that when it returns the JSON objects they have those fields already concatenated so that the app can just use that URL:
"http://wwww.mysite.com/[data field1]/[datafield2]"
The query you should be trying looks like
$query = "SELECT col1,col2 FROM table";// no need of asterisk if you mention col names
Now if you need to combine two columns there itself in the query, try something like
$query = "SELECT CONCAT('mysite.com/',col1,'/',col2) FROM table";
"/" is the separator between two columns.
The query to fetch individual cols is
select col1,col2,col3 from table_name
No need to provide * like you did
$query = "SELECT *[datafield],[otherdatafield],... FROM table";
^........here * is causing the problem.