i am just a beginner and simply trying to read data from a db.there are 2 files, one is include file which contains db connection code and another the 2nd part of my given below code.In db, 3 columns there: 1.id, 2.name and 3. description when i try to open the located folder in firefox it shows
"Fatal error: Call to undefined
function mysql_fetch_arrey() in
C:\xampp\htdocs\www\database
connection\index.php on line 8"
the 8 num line is
"while($person =
mysql_fetch_arrey($result))"
i am checked the "mysql_fetch_arrey($result)" with "$mysql_fetch_arrey($result)"
but it shows
"Fatal error: Function name must be a
string in C:\xampp\htdocs\www\database
connection\index.php on line 8"
i use adobe dreamwaver5 and xampp server
1st page :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>connection</title>
</head>
<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'db_connection';
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
?>
</body>
</html>
</body>
</html>
2nd page :
<?php
include 'connection.php';
$query = "SELECT * FROM people";
$result = mysql_query($query);
while($person = $mysql_fetch_arrey($result))
{
echo "<h3>".$person['name']."</h3>";
echo "<p>".$person['description']."</p>";
}
?>
The function's name is mysql_fetch_array, not mysql_fetch_arrey. So your while loop should like:
while($person = $mysql_fetch_array($result)) {
....
}
Typo here $mysql_fetch_arrey
This should be
while($person = mysql_fetch_array($result))
Related
I'm trying to connect to a PostgreSQL database when I click on a button and display a message to notify success or fail connection.
It's my first time doing such operation in web interface and using Jaxon (PHP lib to do AJAX, fork of XAJAX).
<?php
include "vendor/autoload.php";
use Jaxon\Response\Response;
use Jaxon\jaxon;
class Db_connection
{
public function test() {
$host = "host = localhost";
$port = "port = 5432";
$dbname = "dbname = test";
$user = "user = test";
$password = "password = test";
$response = new Response();
$textFail = "Error : Unable to open database";
$textValid = "Opened database successfully";
$db = pg_connect("$host $port $dbname $user $password" );
if(!$db){
$response->alert($textFail);
return $response;
}else {
$response->alert($textValid);
return $response;
}
}
}
$jaxon = jaxon();
$jaxon->register(Jaxon::CALLABLE_OBJECT, new Db_connection());
$jaxon->processRequest();
?>
<!doctype html>
<html>
<head>
<title>Jaxon Simple Test</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="/favicon.ico">
</head>
<body>
<input type="button" value="Submit" onclick="JaxonDb_connection.test()" />
</body>
<?php
echo $jaxon->getJs();
echo $jaxon->getScript();
?>
</html>
When I click the button from my browser I get nothing.
I want to learn from this, so, if possible, explain to me what I'm doing wrong here, or if my approach is lacking insight.
Checkout the pg_connect() documentation at https://www.php.net/manual/fr/function.pg-connect.php. You need to fix its parameters.
You may also want to use the developer mode of your browser to see when your application return an HTML error instead of the expected json, which in this case explains why you get nothing in your web page.
Thierry
I have an existing SHTML page with a few INCLUDE statements for menu's. This has worked well. To this point, my .htaccess file has looked like:
Options +FollowSymLinks
And my Include statement looked like this:
<!--#include virtual="menu_primary.shtml" -->
I have a new need to add some PHP code to the main page. The PHP code queries a Mysql database to return a single row. If the row exists in the database, I want to show it on the SHTML page. If the row does not exist, do nothing. Here's the PHP code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT notice from notification where page = 'home'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Message is: " . $row["notice"]. "<br>";
}
} else {
echo "";
}
mysqli_close($conn);
?>
</body>
</html>
When I implemented this PHP code, the page seemed to interpret everything after the Greater Than symbol as text. When I posted that problem, someone on this forum suggested altering the .htaccess file to include a PHP parser. For a while. I altered my .htaccess file to look like this:
Options +FollowSymLinks
<FilesMatch "\.(htm|html|shtm|shtml)$">
SetHandler application/x-httpd-php5
</FilesMatch>
However when I do that, the PHP code works fine and I display the data from the database on the SHTML page, but the #Include statements no longer work. How can I enable both the PHP and #Include code together in the same SHTML page? Thanks very much for looking at this.
In your PHP script you can invoke the virtual function to work with your SSI
<?php
virtual('menu_primary.shtml');
There's a very old page that talks about this in more detail
http://www.zytrax.com/tech/php/php_ssi.htm
here is what I want but cannot make it work with the new MySQLi just because my host does not have all new php etc...
But there must be some kind of solution or thats all MYSQLI can do ?
Please dont talk about PDO because even the ugly name sounds like PEDO and I am only interested in MySQLI and a solution for this simple thing. Please dont change the structure of my script. The question is only is if there is something to make it work with MySQL or if I maybe switch back to MySQL procedure instead of statements
<?php
$sql = new mysqli('localhost','user','pass','db');
$who = $_GET['user'];
$query = $sql->prepare("SELECT * FROM profiles WHERE user=?");
$query->bind_param("s",$who);
$result = $query->execute();
while($row = $result->fetch_array(MYSQLI_ASSOC)) // << HERE IS THE PROBLEM //
// I GET ERROR Fatal error: Call to a member function fetch_array() on a non-object in ... //
"" but there must be some way to make it work like we do with while($row = mysqli_fetch_array($sql)) //
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title</title>
</head>
<body>
CUSTOM HTML FOR A NICE DESIGN I WANT TO KEEP THE SAME DESIGN LAYOUT ETC...
HELLO <?php echo "$row[USERNAME]"; ?>
YOUR INFO IS <?php echo "$row[JUST_SOME_INFO]"; ?>
</body>
</html>
<?php
}
?>
Here is your solution
You don't have to use fetch_array() but use fetch()
also you will have to bind result using bind_result before fetch
$query->execute();
$query->bind_result($col1);
while($row = $query->fetch()){
printf("%s \n", $col1);
}
You can use $col1 directly also, no need to use it with printf()
My working example according to my db is given below if you want more assistance,
$sql = new mysqli('localhost','user','pass','dbname');
$who = 'php';
$query = $sql->prepare("SELECT * FROM job WHERE skill=?");
$query->bind_param("s",$who);
$query->execute();
$query->bind_result($col1);
while($row = $query->fetch()){
printf("%s \n", $col1);
}
Comment this string:
$result = $query->get_result();
you don't need this.
Try this one.
<?php
$sql = new mysqli('localhost', 'user', 'pass', 'db');
$who = $_GET['user'];
$query = $sql->prepare("SELECT * FROM profiles WHERE user=?");
$query->bind_param("s", $who);
$result = $query->execute() or die($query->error);
while ($row = $result->fetch_array()) {
$username = $row[USERNAME];
$info = $row[JUST_SOME_INFO];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title</title>
</head>
<body>
CUSTOM HTML FOR A NICE DESIGN I WANT TO KEEP THE SAME DESIGN LAYOUT ETC...
HELLO <?php echo $username; ?>
YOUR INFO IS <?php echo $info; ?>
</body>
</html>
This is what happens when you start copying and pasting php together without knowing what it means.
the solution here is:
create a class for $sql or declare $sql a global imediately after calling the variable.
try
global $sql;
but if you actually want to fix the problem(by making your $sql variable an object)
try
$sql = array();
$sql['host'] = 'host';
$sql['user'] = 'user';
$sql['pass'] = 'password';
$sql['db'] = 'database';
global $sql;
$db = new mysqli($sql);
Now when you wanna do something with the db connection.
$db->action();
this is just the basics and probably won't work. you probably should instead read more on php/mysqli before attempting to write your own classes.
This is the "Registration Successful" page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration Successful</title>
</head>
<body>
<p>
<?php
// Make a MySQL Connection
mysql_connect("localhost", "userid", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$name=$_POST['name'];
$age=$_POST['age'];
// These only work for CRYPT_SHA512, but it should give you an idea of how crypt() works.
$Salt = uniqid(); // Could use the second parameter to give it more entropy.
$Algo = '6'; // This is CRYPT_SHA512 as shown on http://php.net/crypt
$Rounds = '10000'; // The more, the more secure it is!
// This is the "salt" string we give to crypt().
$CryptSalt = '$' . $Algo . '$rounds=' . $Rounds . '$' . $Salt;
$hashed_password = crypt($_POST['password'], $CryptSalt);
// Insert a row of information into the table "example"
mysql_query("INSERT INTO example
(name, age, password) VALUES('$name', '$age', '$hashed_password' ) ")
or die(mysql_error());
echo "Data Inserted!";
?>
</p>
<p>Click here to Login!</p>
</body>
</html>
And this is Login Check Page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login Check</title>
</head>
<body>
<p>
<?php
// Make a MySQL Connection
mysql_connect("localhost", "userid", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$name=$_POST['name'];
// These only work for CRYPT_SHA512, but it should give you an idea of how crypt() works.
$Salt = uniqid(); // Could use the second parameter to give it more entropy.
$Algo = '6'; // This is CRYPT_SHA512 as shown on http://php.net/crypt
$Rounds = '10000'; // The more, the more secure it is!
// This is the "salt" string we give to crypt().
$CryptSalt = '$' . $Algo . '$rounds=' . $Rounds . '$' . $Salt;
$hashed_password = crypt($_POST['password1'], $CryptString);
$result = mysql_query("SELECT * FROM example WHERE name = '$name'");
$row = mysql_fetch_array($result);
if($row["name"]==$name && crypt($row["password"], $hashed_password) == $hashed_password){
echo"Hello $name !!!";
}
else{
echo"Sorry, your credentials are not valid, Please try again.";
}
?>
</p>
</body>
</html>
The problem is that I am getting the following result when I try to log in with the same name and password which I used while signing up:
Sorry, your credentials are not valid, Please try again.
Can anybody tell what the problem is? My question might be a silly one but I am an entry level programmer and I really need help.
Thanks a lot in advance.
HERE IS THE REVISED CODE WHERE I SAVED THE SALT AT SIGN UP AND RETRIEVED AT LOGIN:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login Check</title>
</head>
<body>
<p>
<?php
// Make a MySQL Connection
mysql_connect("localhost", "userid", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$name=$_POST['name'];
// These only work for CRYPT_SHA512, but it should give you an idea of how crypt() works.
//$Salt = uniqid(); // Could use the second parameter to give it more entropy.
//$Algo = '6'; // This is CRYPT_SHA512 as shown on http://php.net/crypt
//$Rounds = '10000'; // The more, the more secure it is!
// This is the "salt" string we give to crypt().
//$CryptSalt = '$' . $Algo . '$rounds=' . $Rounds . '$' . $Salt;
$result = mysql_query("SELECT * FROM example WHERE name = '$name'");
$row = mysql_fetch_array($result);
$CryptSalt = $row["salt"];
$hashed_password = crypt($_POST['password1'], $CryptSalt);
if($row["name"]==$name && crypt($row["password"], $hashed_password) == $hashed_password){
echo"Hello $name !!!";
}
else{
echo"Sorry, your credentials are not valid, Please try again.";
}
?>
</p>
</body>
</html>
When logging in you should query hashed password from database and use explode to get salt from there since it is part of the hashed password. Alternatively you can save the salt in different field and retrieve it from there or hardcode to use same salt for every password.
Use
if($row["name"]==$name and crypt($_POST['password1'], $row["password"]) == $row['password']) {
...
You don't need to save the salt in a special column as it is already included in the output of crypt e.g. crypt('secret', '$6$mysalt$') gives "$6$mysalt$UX6P1V...". It's a feature of crypt() that you can pass the old hash as $salt parameter to let it use the same salt.
I have managed to get part of this code working.
The variables will echo without problem but as soon as I try and put them into the javascript tag they stop working.
What am I doing wrong??
<?php
$id_1 = $_GET['id'];
$tag_id = "tag_id";
$Activity_Tag_String = "Activity_Tag_String";
$group_tag_string = "group_tag_string";
$link = mysqli_connect("localhost", "username", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT $tag_id, $Activity_Tag_String, $group_tag_string, advertiser_id FROM tbl_tags WHERE advertiser_id = '$id_1'";
$result = mysqli_query($link, $query);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Mate Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
var branch = window.location.href;
var axel = Math.random() + "";
var a = axel * 10000000000000;
document.write('<iframe src="http://fls.doubleclick.net/activityi;src=<?php $tag_id?>;type=<?php $Activity_Tag_String?>;cat=<?php $group_tag_string?>;u1='';ord=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>');
</script>
</body>
</html>
<?php $tag_id?>
should be
<?= $tag_id ?>
or
<?php echo $tag_id ?>
Your version is simply doing the PHP equivalent of:
$tag_id;
which is a do-nothing statement. You need to actually echo that variable's contents.
As well, note that your code is vulnerable to SQL injection attacks, and you should fix that before you do anything else.
for js script part try:
<?php echo $variable;?>
or in short
<?=$var?>