I'm trying to query some vars in my MySQL db, but for some reasons it returns 0.
Here's the code:
<html>
<body>
<?php
session_start();
$connection = mysqli_connect("", "", "", "");
if (empty($connection))
$error = "Could not connect to Database, please contact an admin with this code: DBE1";
$tnews = mysqli_query($connection, "SELECT * FROM Test ORDER BY ID DESC");
while ($news = mysqli_fetch_array($tnews))
{
$title = $news['Title'];
$pimg = $news['PrevImg'];
$ptext = $news['PrevText'];
$text = $news['Text'];
$img1 = $news['Img1'];
echo "<h1>" + $title + "</h1><br><br>";
echo "<p>" + $pimg + "</p>";
echo "<br><p>" + $ptext + "</p><br><br>";
echo "<p>" + $text + "</p>";
echo "<br><p>" + $img1 + "</p>";
echo $title;
}
mysqli_close($connection);
?>
</body>
</html>
Oh and the result
00000
I have another site where almost (var names, echo's, ...) everything is the same, it works there! I have really have no idea what's wrong, does anyone have an idea?
P.S: The DB Connection has empty strings because it has the ip, username, password and db of my database! ;D
Thanks for the help!
The + is a concatenate operator in JS, use PHP's equivalent, being a dot .
Now, as stated in comments, you are outputting content before sessions.
You may get a warning similar to this:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /path/to/file:3) in /path/to/file.php on line 4
Corrected code, is you still want to use sessions. If not, just remove session_start();
Sidenote: It is important not to have a space before <?php or any other output such as HTML, a cookie or a byte order mark, which will also account as outputting before header.
<?php
session_start();
?>
<html>
<body>
<?php
$connection = mysqli_connect("", "", "", "");
if (empty($connection))
$error = "Could not connect to Database, please contact an admin with this code: DBE1";
$tnews = mysqli_query($connection, "SELECT * FROM Test ORDER BY ID DESC");
while ($news = mysqli_fetch_array($tnews))
{
$title = $news['Title'];
$pimg = $news['PrevImg'];
$ptext = $news['PrevText'];
$text = $news['Text'];
$img1 = $news['Img1'];
echo "<h1>" . $title . "</h1><br><br>";
echo "<p>" . $pimg . "</p>";
echo "<br><p>" . $ptext . "</p><br><br>";
echo "<p>" . $text . "</p>";
echo "<br><p>" . $img1 . "</p>";
echo $title;
}
mysqli_close($connection);
?>
</body>
</html>
Add error reporting to the top of your file(s) which will signal warnings/errors as shown further up in my answer.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
I don't know how that worked out in your other server, but you should be using dots . instead of + to concatenate strings in php.
while ($news = mysqli_fetch_array($tnews))
{
$title = $news['Title'];
$pimg = $news['PrevImg'];
$ptext = $news['PrevText'];
$text = $news['Text'];
$img1 = $news['Img1'];
echo "<h1>" . $title . "</h1><br><br>";
echo "<p>" . $pimg . "</p>";
echo "<br><p>" . $ptext . "</p><br><br>";
echo "<p>" . $text . "</p>";
echo "<br><p>" . $img1 . "</p>";
echo $title;
}
Related
I have this code and in "SELECT * FROM Blogi WHERE PostID=".
How to get each line separately so that takes postedBY, title, date, content by PostID
<?php
try{
$Blog = $conn->prepare("SELECT * FROM Blogi WHERE PostID=");
$Blog->execute();
}catch(PDOException $e){
echo $e->getMessage();
}
$fetch = $Blog->fetchAll();
$usercount = $Blog->rowCount();
if($usercount > 0){
foreach($fetch as $f){
$PostID = $f['PostID'];
$PostedBY = $f['PostedBY'];
$Title = $f['Title'];
$Date = $f['Date'];
$Content = $f['Content'];
}
}else{
session_destroy();
header('location: index.php');
}
?>
I have very bad English, so I don't know anyone can understand this.
How to do it further this postID="" to that he would start to work?
You have already done it in foreach loop... so add a line for output like... Also you aren't sending any PostID value to your query, be sure that you are sending a value for it.
foreach($fetch as $f){
$PostID = $f['PostID'];
$PostedBY = $f['PostedBY']
$Title = $f['Title'];
$Date = $f['Date'];
$Content = $f['Content'];
echo $PostID . " " . $Title . " " . $Date . " " . $Content . "<br />";
}
How do I need to adjust the below php if the output of the php-script needs to be saved in $content?
<?php
//db verbindung
mysql_connect("Hostname", "Username", "Password");
mysql_select_db("Database Name");
//db abfrage
$query = "SELECT
YEAR(datetime) AS dy,
MONTH(datetime) -1 AS dm,
DAY(datetime) AS dd,
HOUR(datetime) AS th,
MINUTE(datetime) AS tm,
temp,
hum,
pressure
FROM wettertabelle
WHERE DATE(datetime) = '2013-11-25'
ORDER BY datetime
";
// NEU: Variable definieren
$zeilenzaehler = 1;
//ausgabe der zeilen
$result = mysql_query($query)
OR die("Error: $query <br>" . mysql_error());
while ($row = mysql_fetch_array($result)) {
// echo
if ($zeilenzaehler != 1) {
echo ",";
}
echo "{date: new Date(" . $row['dy'] . "," . $row['dm'] . "," . $row['dd'] . "," . $row['th'] . "," . $row ['tm'] . "),t:" . $row['temp'] . ",h:" . $row['hum'] . ",p:" . $row['pressure'] . "}";
//Variable jetzt auf 2
$zeilenzaehler = 2;
};
?>
What is working is e.g.:
<?php
$content = This is the output!!
?>
or
<?php
ob_start();
echo 'This is the output!!';
$content=ob_get_contents();
ob_end_clean();
?>
How would I need to adjust the above script to get it into the following output structure:
<?php
class user_klasse
{
function ausgabe()
{
return 'This test is generated by php.';
}
}
?>
You only have two echo statements, change them to save the value in your variable. No need for output buffering for something so small.
if ($zeilenzaehler != 1)
{
$content.= ","; // instead of echo, and same for the one below
}
$content.= "{date: new Date(".$row['dy'].",".$row['dm'].",".$row['dd'].",".$row['th'].",".$row ['tm']."),t:".$row['temp'].",h:".$row['hum'].",p:".$row['pressure']."}";
At the end you can just do
echo $content;
I have written up some code to take a upc entry from a text box and upon submit will pass the "upc" to the query code.
My problem is, none of my echos will post to my page. I can get them to post in CLI, but not on my web browser. If I put in test echos before the fetch line of the query they will echo to the page, but anything after the query will not. Here is the code:
<?php
//setup
require_once('Factual.php');
$factual = new Factual("mykeyhere","myotherkeyhere");
$upc = $_GET["upc"];
$query = new FactualQuery;
$query->search($upc);
$res = $factual->fetch("products", $query);
$data = $res->getData();
//print_r($res->getData());
$item = $data[1];
$itemAvgPrice = $item['avg_price'];
$itemBrand = $item['brand'];
$itemCategory = $item['category'];
$itemEan13 = $item['ean13'];
$itemFactualID = $item['factual_id'];
$itemImage = $item['image_urls'][0];
$itemSize = $item['size'][0];
$itemUPC = $item['upc'];
echo "Average Price: " . $itemAvgPrice;
echo "\nBrand: " . $itemBrand;
echo "\nCategory: " . $itemCategory;
echo "\nEan13: " . $itemEan13;
echo "\nFactual ID: " . $itemFactualID;
echo "\nImage URL: " . $itemImage;
echo "\nSize: " . $itemSize;
echo "\nUPC: " . $itemUPC;
?>
I have a table
Now.i have a function in my JS
function add()
{
<?php
include('conn.php');
$rs = mysql_query("select * from position");
$row = mysql_fetch_array($rs);
$ss=$row['Name'];
$sss=$row['nowb'];
$ssss=$row['totalb'];
$sssss=$row['nowc'];
$ssssss=$row['totalc'];
echo "add2()";
?>}
function add2(){
AddAddress("<?php echo $ss;?>","<?php echo $sss;?>/<?php echo $ssss;?><br /><?php echo $sssss;?>/<?php echo $ssssss;?>");
}
How to get the every date from my table?
Something like this?
function add() {
<?php
include('conn.php');
$rs = mysql_query("select * from position");
while ( $row = mysql_fetch_array($rs) ) {
$ss=$row['Name'];
$sss=$row['nowb'];
$ssss=$row['totalb'];
$sssss=$row['nowc'];
$ssssss=$row['totalc'];
echo 'AddAddress("' . $ss . '","' . $sss . '/' . $ssss . '<br />' . $sssss . '/' . $ssssss . '");';
}
?>
}
Didn't text the echo 'AddAddress....' line so I hop eI got all the single and double quotes in the right place??
Performing POST requests using Ajax here is an example of sending data from js to php.
also stop naming your vars s,ss,sss,ssss you will have no idea what they mean when you read your code tomorrow..
and try not to use mysql_* functions they have been deprecated switch to mysqli or pdo
I got what would you like to do. In your PHP file:
function add(){
<?php
include('conn.php');
$rs = mysql_query("select * from position");
echo "var data = [] ; "
while($row = mysql_fetch_assoc($rs)){
echo "
data.push({
name: '{$row['Name']}',
nowb: '{$row['nowb']}',
totalb: '{$row['totalb']}',
nowc: '{$row['nowc']}',
totalc: '{$row['totalc']}'
}); \n\r " ;
}
?>
add2(data);
}
function add2(data){
for (var i in data){
var row = data[i] ;
AddAddress(row.name, row.nowb, row.totalb, row.nowc, row.totalc);
}
}
If I understand the question correctly you want to know how to loop through an array in php.
$row = mysql_fetch_array($rs);
foreach($row as $value){
//Do something
}
Read up on it in the docs
http://php.net/manual/en/control-structures.foreach.php
I'm trying to build an active page menu with PHP and MySQL and am having a difficult time fixing the error. In the while statement I have an if statement that is giving me fits. Basically I think I'm saying that "thispage" is equal to the "title" based on pageID and as the menu is looped through if "thispage" is equal to "title" then echo id="active".
Thanks
<?php
mysql_select_db($database_db_connection, $db_connection);
$query_rsDaTa = "SELECT * FROM pages WHERE pagesID = 4";
$rsDaTa = mysql_query($query_rsDaTa, $db_connection) or die(mysql_error());
$row_rsDaTa = mysql_fetch_assoc($rsDaTa);
$totalRows_rsDaTa = mysql_num_rows($rsDaTa);
$query_rsMenu = "SELECT * FROM menu WHERE online = 1 ORDER BY menuPos ASC";
$rsMenu = mysql_query($query_rsMenu, $db_connection) or die(mysql_error());
$thisPage = ($row_rsDaTa['title']);
?>
<link href="../css/MainStyle.css" rel="stylesheet" type="text/css" />
<h2><?php echo $thisPage; ?></h2>
<div id="footcontainer">
<ul id="footlist">
<?php
while($row_rsMenu = mysql_fetch_assoc($rsMenu)) {
echo (" <li" . <?php if ($thisPage==$row_rsDaTa['title']) echo id="active"; ?> . "" . $row_rsMenu['menuName'] . "</li>\n");
}
echo "</ul>\n";
?>
</div>
<?php
mysql_free_result($rsMenu);
mysql_free_result($rsDaTa);
?>
kind of a big, hairy line. i think you need to make it a little easier by splitting it into more than one line. also, what is this part of your line supposed to do?
echo id="active";
do you mean
echo " id=\"active\" ";
note i added a space before "id" because you don't have one after the LI
Parse errors can be located by consecutive removing various blocks of code.
Remove some portions of your code and see, if error persists.
Say, you can temporarily remove html part. If error got eliminated - it's in this part. Now you can divide this part on smaller blocks and so on. Thus you can locate an erroneous line pretty close.
Also, the error message usually contains some vital information on the error.
First, you have <?php nested inside another <?php. This causes:
syntax error, unexpected '<'
Let's remove both <?php and ?>. Now I see that you want you output the id, but you don't tell PHP that it's a string. Wrap it in single quotes so that echo id="active"; becomes echo ' id="active"';
With this out of the way, you can't contatenate an if statement just like that:
echo (" <li" . if ($thisPage==$row_rsDaTa['title']) echo ' id="active"'; ...
You might want to introduce a variable that will store the string id="active" if you're on the current page.
$id = '';
if ($thisPage==$row_rsDaTa['title']) {
$id = ' id="active"';
}
This piece of code might look like this when rewritten:
while($row_rsMenu = mysql_fetch_assoc($rsMenu)) {
$active = '';
if ($thisPage==$row_rsDaTa['title']) {
$id = ' id="active"';
}
echo (" <li" . $id . "<a href=\"../" . $row_rsMenu['menuURL'] . "\">" .
$row_rsMenu['menuName'] . "</a></li>\n");
}
echo "</ul>\n";
Try this :
<?php
mysql_select_db($database_db_connection, $db_connection);
$query_rsDaTa = "SELECT * FROM pages WHERE pagesID = 4";
$rsDaTa = mysql_query($query_rsDaTa, $db_connection) or die(mysql_error());
$row_rsDaTa = mysql_fetch_assoc($rsDaTa);
$totalRows_rsDaTa = mysql_num_rows($rsDaTa);
$query_rsMenu = "SELECT * FROM menu WHERE online = 1 ORDER BY menuPos ASC";
$rsMenu = mysql_query($query_rsMenu, $db_connection) or die(mysql_error());
$thisPage = ($row_rsDaTa['title']);
?>
<link href="../css/MainStyle.css" rel="stylesheet" type="text/css" />
<h2><?php echo $thisPage; ?></h2>
<div id="footcontainer">
<ul id="footlist">
<?php
while($row_rsMenu = mysql_fetch_assoc($rsMenu)) {
$id = ($thisPage==$row_rsDaTa['title']) ? "id='active'" : "";
echo "<li " . $id . "<a href='../" . $row_rsMenu['menuURL'] . "' >" . $row_rsMenu['menuName'] . "</a></li>\n";
}
?>
</ul>
</div>
<?php
mysql_free_result($rsMenu);
mysql_free_result($rsDaTa);
?>