Recently I've bought webhosting at names.co.uk and I'm trying to set up something simple which will display the name from a table named Team if the id = 1.
This is my code
<?php
$q = "SELECT * FROM `Team` WHERE id =1";
$result = mysql_query($q);
echo '<br />Query is send';
echo '<br />Result is true';
$row = mysql_fetch_array($result);
echo '<br />tryed fetching row';
if ($row === FALSE) {
echo '<br />$row is not false.';
$name = $row['name'];
echo '<br />$name now is "' . $name . '"';
}
else {
echo( mysql_error());
}
echo $name;
?>
This is the output:
Query is send Result is true tryed fetching rowNo such file or
directory
UPDATE:
I have changed to msqli:
$q = "SELECT * FROM `Team` WHERE id =1";
$result = mysqli_query($q);
echo '<br />Query is send';
echo '<br />Result is true';
$row = mysqli_fetch_array($result);
echo '<br />tryed fetching row';
if ($row !== FALSE) {
echo '<br />$row is not false.';
$name = $row['name'];
echo '<br />$name now is "' . $name . '"';
}
else {
echo( mysqli_error());
}
echo $name;
and now I'm getting this output:
Query is send Result is true tryed fetching row $row is not false.
$name now is ""
You need to fist establish a connection. For example: $connection = mysqli_connect($servername, $username, $password);.
See this link on how to use MySQLi: https://www.w3schools.com/PHP/php_mysql_connect.asp (but note that w3schools is a bad resource, with outdated information and bad practices - I'm only linking to it because this tutorial is basic and clear).
Be sure to check later, if you still haven't, on how to properly sanitize your queries. See this, for example: How can I prevent SQL injection in PHP?
Use function:
$result = mysqli_query($q);
mysql_query() have been deprecated in PHP7 onwards.
Hi I am working on trying to get my app to load json files from a folder and decode to a database using the following code :
<?php
$con = mysqli_connect("localhost", "root", "", "json_map");
$response = array();
$res = array();
foreach(glob('C:\xampp\htdocs\laravel\awsconfig\app\JSON_Files') as $filename) {$json = file_get_contents($filename);
if ($json != null) {
$decoded = json_decode($json, true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];
if (is_array($decoded["configurationItems"])) {
foreach ($decoded["configurationItems"] as $configurationItems)
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)
{
$configurationItemVersion = $configurationItems["configurationItemVersion"];
echo "<br />", "configuration_Item_Version:", $configurationItemVersion, "<br />";
$configurationItemCaptureTime = $configurationItems["configurationItemCaptureTime"];
echo "configurationItemCaptureTime:", $configurationItemCaptureTime, "<br />";
$configurationStateId = $configurationItems["configurationStateId"];
echo "configurationStateId:", $configurationStateId, "<br />";
$awsAccountId = $configurationItems["awsAccountId"];
echo "awsAccountId:", $awsAccountId, "<br />";
$configurationItemStatus = $configurationItems["configurationItemStatus"];
echo "configurationItemStatus:", $configurationItemStatus, "<br />";
$resourceId = $configurationItems["resourceId"];
echo "resourceId:", $resourceId, "<br />";
$ARN = $configurationItems["ARN"];
echo "ARN:", $ARN, "<br />";
$awsRegion = $configurationItems["awsRegion"];
echo "awsRegion:", $awsRegion, "<br />";
$availabilityZone = $configurationItems["availabilityZone"];
echo "availabilityZone:", $availabilityZone, "<br />";
$configurationStateMd5Hash = $configurationItems["configurationStateMd5Hash"];
echo "configurationStateMd5Hash:", $configurationStateMd5Hash, "<br />";
$resourceType = $configurationItems["resourceType"];
echo "resourceType:", $resourceType, "<br />";
$resourceCreationTime = $configurationItems["resourceCreationTime"];
echo "resourceCreationTime:", $resourceCreationTime, "<br />";
$result = mysqli_query($con, "INSERT INTO configuration_item(configuration_item_version,configuration_item_capture_time,configuration_state_id, aws_account_id, configuration_item_status, resource_id, arn, aws_region, availability_zone,configuration_state_md5_hash, resource_type, resource_creation_time)
VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId','$awsAccountId','$configurationItemStatus','$resourceId','$ARN','$awsRegion','$availabilityZone','$configurationStateMd5Hash','$resourceType','$resourceCreationTime' )") or die("Insert Failed " . ((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
;
}
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully stored configuration items ";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
?>
I know it will be something stupid like a bracket but even using an IDE I am still no seeing it
Other than the missing foreach on line 7 (I'm assuming a mistake, because it would give a different syntax error message).
Your actual syntax error is a missing closing brace. Add } to the end of your code before the closing ?> and you have valid syntax. Whether you have working code is another matter. Try formatting your code with better indentation and you will more easily see where the missing tokens should be.
As a side note: A closing ?> is not required in PHP and I recommend against it.
I have a user table that contain a lots more data, I wonder how can I improve my select code below
if ($result = $db->query("SELECT * FROM user ")) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row["name"] . "<br />";
echo $row["user_id"] . "<br />";
echo $row["photo"] . "<br />";
//.. a lot more column here
}
}
I use this as a array: $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM `database` WHERE 1));
and to call it i use $user_info['column']
I have a domain where users have a subdomain with its own name.
Example:
User: Vitor
Subdomain: vitor.google.com
The database has the column user table my_users and have the columns birthday, address, password, city and state in table users_infos.
How do I do that in PHP in index.php can get this information direct user to the database?
this is my incorrect code:
// Get Subdomain
$urlExplode = explode('.', $_SERVER['HTTP_HOST']);
if (count($urlExplode) > 2 && $urlExplode[0] !== 'www') {
$subdomain = $urlExplode[0];
echo $subdomain;
}
// Select DB
$sql = "SELECT * FROM users_infos i INNER JOIN my_users u on u.id = i.id where u.users='$users'";
$result = mysql_query($sql);
if($result === FALSE) {
die(mysql_error());
// TODO: better error handling
}
else {
$row = mysql_fetch_array($result);
$userTitleSite = $row['userTitleSite'];
echo $id_textos = $row["id_textos"];
echo "<br />";
echo $id = $row["id"];
echo "<br />";
echo $phone = $row["phone"];
echo "<br />";
echo "<br />";
}
// Says that the subdomain is = user
$subdomain = $user;
Correct answer below:
Using the help you gave me, it looks like I got here ..
follows the correct code I could do for future users who have the same question:
// Get subdomain
$urlExplode = explode('.', $_SERVER['HTTP_HOST']);
if (count($urlExplode) > 2 && $urlExplode[0] !== 'www') {
$subdomain = $urlExplode[0];
echo $subdomain;
}
// Says that the subdomain is = user
$user = $subdomain;
// Select DB
$sql = "SELECT * FROM vms_textos i INNER JOIN vms_users u on u.id = i.id where u.user='$user'";
$result = mysql_query($sql);
if($result === FALSE) {
die(mysql_error());
// TODO: better error handling
}
else {
$row = mysql_fetch_array($result);
$userTitleSite = $row['userTitleSite'];
echo "<br />";
echo $id_textos = $row["id_textos"];
echo "<br />";
echo $user= $row["user"];
echo "<br />";
echo $id = $row["id"];
echo "<br />";
echo $telefone = $row["telefone"];
echo "<br />";
echo "<br />";
}
echo "<br />";
echo "<br />";
Thanks all for help me! :)
$subdomain = $user;
Should go before the $sql, as your code is written $user is empty.
Are you sure the WHERE clause u.id = i.id is correct? In other words, do the ID columns in your my_users and users_infos table match? It is more common to have a foreign key reference on the users_infos table (e.g. my_users_id), in which case the query would be something like:
SELECT *
FROM users_infos i
INNER JOIN my_users u on u.id = i.my_users_id
WHERE u.users='$users'
I have a code which fetches data from a mysql table and converts it into pdf document, the code is working fine except it is skipping row 1.
Here is the code from which i have removed the pdf generation process since the problem is in the loop which is fetching data.
Please help.
<?php
session_start();
if(isset($_SESSION['user']))
{
$cr = $_POST['cour'];
$s = $_POST['sem'];
require('fpdf.php');
include('../includes/connection.php');
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(!mysql_fetch_array($rs))
{
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
}
else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
$formno[$i] = $row ['FormNo'];
$rno[$i] = $row ['rollno'];
$snm[$i] = $row ['StudentNm'];
$fnm[$i] = $row ['FathersNm'];
$mnm[$i] = $row ['MothersNm'];
$addr[$i] = $row['Address'];
$pic[$i] = $row['imagenm'];
$comm[$i] = $row['SocialCat'];
echo $formno[$i]."<br />";
echo $rno[$i]."<br />";
echo $snm[$i]."<br />";
echo $fnm[$i]."<br />";
echo $mnm[$i]."<br />";
echo $addr[$i]."<br />";
echo $pic[$i]."<br />";
echo $comm[$i]."<br />";
echo "<br />";
}
}
mysql_close($con);
}
?>
You are fetching the first row outside of your for() loop then you miss it.
After mysql_query() your should use mysql_num_rows() to check if there are any rows in your result and then fetch them in the for loop.
More info here : http://php.net/manual/fr/function.mysql-num-rows.php
Your code would look like this :
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(0 == mysql_num_rows($rs)) {
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
} else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
// Your code
}
}