Jump to content

Eggzorcist

Members
  • Posts

    214
  • Joined

  • Last visited

    Never

Posts posted by Eggzorcist

  1. The way I would go about doing this myself would be do a friend1 = username, friend2 = username then loop through the friend1 data with logic parameters singling them out. I was just wondering whether this is an efficient method to do this? What's another way?

  2. Hi there,

     

    I have a question concerning MYSQL.

     

    I have a friend list with 3 fields, id, friend1, friend2. This highlights all the members friend relations among each other.

     

    My problem in making a friendlist is that it's difficult making a friendlist query. I'm wondering if I can select friend1 when friend2 = $username and select friend 2 when $friend1 = $username for each row. Was wondering if this is possible?

     

    Thanks

  3. Hi everyone.

     

    I'm making a social media type app and was wondering how to deal with large amounts of friends per user. My first guess was to create a large text field where I append all the names the person is friends with. Similarly to this: (friend1, friend2, friend3) Though I feel as though this isn't the best and more efficient way to go about it. THen I thought maybe I should create a new table entirely of maybe 3 fields containing (id, friend1, friend2). Where then I can fish out who's friends with who.

     

    I'm wondering if there is any way, or better way. And which of the two is the best way? How does a site like Facebook deal with this?

     

    Thank you?

  4. Hi there, I was just wondering if there was a straightforward command to fetch info from the db but only unique fields? For example, I have a product list and each products belongs to a category that is user generated. I want to make a category list. but there are hundreds of categories with maybe 8-15 categories. How can I fetch the categories while only fetching 1 variable per category?

     

    If someone can point me in the right direction that would be great!

  5. Here's the breakdown.

     

    I have form 1

     

    Asks you how many fields?

    turns into $_post['howmany'];

     

    then I loop the form how many times...

     

    But when I put it into the database, I want to keep the howmany variable. I use the same $_post but its unidentified since a new form has been submitted... How can I transmit this variable to the second submit and second script. Both forms are $_POST $_SERVER[php_SELF].

  6. SO here's my deal. I have a page with 2 submission buttons. The first, ask the number of fields required for the dataset. Which that submit1 creates the next form on the same page. Though, I tried re-using the $_POST variable to keep the amount of fields created to determine the amount of loops to go through for submission. But my problem is when the second submit button is pressed, that number variable is undefined. My question is how can I transmit the variable from the first form to the second?

     

    Thanks

  7. It might be that there are no replies for the purpose that it isn't any technical help with programming itself. It seems as though it's a request, in which case, you may need to hire a programmer.

     

    If on the other hand, you have a SPECIFIC question and have tried programming it yourself, and need assistance in seeing what went wrong, then you can post that.

     

    Not trying to be narcissistic or anything.

  8. I have a question concerning outputting information taken out of the database.

     

    Query is as follows:

     

    $query = "SELECT * FROM users";
    	$results = $this->db->query($query);
    	$result = $results->fetch_assoc();
    
    	return $result['username'];

     

     

    The value that returns is the first username in the database. But here I am selecting all the usernames, can I select others ones with the fetch_assoc?

  9. I've been starting to play around the mysqli class and I've been having trouble using it due to various error it gives me from simple queries like this one. I'm not sure what the error is really, I've been following the php manual. Any help would be greatly appreciated.

     

     

     

    <?php 
    
    
    $mysqli = new MySQLi('localhost', 'root', 'root', 'jaipai');
    
    if ($mysqli->connect_errno)
    {
    	echo "There was a connection error: ". $mysqli->connecterrno;
    }
    
    
    
    class testClass 
    
    {
    
    private $db;
    
    
    function __construct($mysqli)
    {
    	$this->db = $mysqli;
    }
    
    public function pageInfo()
    {
    	$query = "SELECT * FROM users WHERE username = jaipai";
    	$results = $this->db->query($query);
    
    	$result = $this->db->fetch_assoc($results);
    
    	return $result['username'];
    
    }
    
    }
    
    $testClass = new testClass($mysqli);
    
    echo $testClass->pageInfo();
    
    
    ?>

     

    This gives me this error:

    Fatal error: Call to undefined method mysqli::fetch_assoc() in /Users/JPFoster/Sites/Research & Development/Programs/Object Sandbox/DatabaseConnection.php on line 30

     

     

     

     

    Just to be a little more informative I've also tried this method

    	$results = $this->db->query($query);
    
    	$result = $results->fetch_assoc();
    
    	return $result['username'];
    

     

    This gives me an error:

    Fatal error: Call to a member function fetch_assoc() on a non-object in Sites/Research & Development/Programs/Object Sandbox/DatabaseConnection.php on line 30

     

     

    I'm not sure which is on the best path to go. Any help would be greatly appreciated.

     

  10. I'm creating a login that uses PDO statements, however when I try to count how many rows was selected, it gives me an error mentioning its an unidentified property, I always tried rowCount, but it did not work either...

     

    Notice: Undefined property: PDOStatement::$num_rows in /Users/JPFoster/Sites/Jaipai.Blog/engine.php on line 50

     

     

    Here's my class method:

     public function login($user, $pw){
    	$user = addslashes($this->user);
    	$pw = md5($pw);
    
    	$query = "SELECT * FROM `users` WHERE `username` = '".$user."' AND `password` = '".$pw."'";
    
    	//set up query
    	$results = $this->pdo->query($query);
    
    
    	if ($results->num_rows == 1){
    	$row = $results->fetch_assoc();
    	echo "Worked!";
    		//$this->set_session($row['id'], $row['username'], $row['email']);
    	} else {
    
    	echo "Your Username and Password did not match"; 	
    
    	}
    
    	//if user + passs match { redirect (member page) and set up sessions
    
    	// Else ... Retry...
    }	

     

     

    Any help with this will be greatly appreciated!

  11. That has helped a lot! Thanks, but now however, I have trouble with my PDO num_rows, is there a reason why its a fairly simplistic code so I'm not sure why it wouldn't work.

     

     

    Notice: Undefined property: PDOStatement::$num_rows in /Users/JPFoster/Sites/Jaipai.Blog/engine.php on line 48

     

  12. Hey, I'm wondering what the issue is, I'm new to using PDO and objects so I'm sure this is a common error but setting me on the right track would be greatly appreciated.

     

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND password = d41d8cd98f00b204e9800998ecf8427e' at line 1' in /Users/JPFoster/Sites/Jaipai.Blog/engine.php:47 Stack trace: #0 /Users/JPFoster/Sites/Jaipai.Blog/engine.php(47): PDO->query('SELECT * FROM u...') #1 /Users/JPFoster/Sites/Jaipai.Blog/index.php(7): SiteEngine->login('', '') #2 {main} thrown in /Users/JPFoster/Sites/Jaipai.Blog/engine.php on line 47

    public function login($user, $pw){
    	$user = addslashes($this->user);
    	$pw = md5($pw);
    
    	$query = "SELECT * FROM users WHERE username = ".$user." AND password = ".$pw;
    
    	//set up query
    	$results = $this->pdo->query($query);
    	$num_rows = $results->num_rows;
    
    	if ($num_rows == 1){
    	$row = $results->fetch_assoc();
    	echo "Worked!";
    		//$this->set_session($row['id'], $row['username'], $row['email']);
    	} else {
    
    	echo "Your Username and Password did not match"; 	
    
    	}
    
    	//if user + passs match { redirect (member page) and set up sessions
    
    	// Else ... Retry...
    }	
    
    
    }

     

    I don't see an issue with my query, and the object is instantiated by having the submit button isset() and i use login($_post[user], $_post[pw])

     

     

  13. Is it possible to use the PDO with a statement that has ORDER BY tablename.

     

    The program works fine but when I added the ORDER BY clause it outputted an error.

     

    Is it maybe because I set the fetch mode to PDO::FETCH_OBJ? Or is that irrelevant?

     

    This is the error that outputs:

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY task_position' at line 1' in /tasks.php(38): PDO->query('SELECT * FROM t...') #1 tasks.php(57): tasks->grab_tasks('2') #2 {main} thrown in tasks.php on line 38

     

    It normally worked without the ORDER BY.

     

    Here is my MYSQL call:

    	
    	$grabber = $this->pdo->query('SELECT * FROM tasks WHERE user_id = ' . $userid . "ORDER BY 'task_position'");	
    	$grabber->setFetchMode(PDO::FETCH_OBJ);

     

     

     

     

  14. I made the changes and I now understand the double referencing. However it still says that the query part of it is a non-object. How is this so?

    Fatal error: Call to a member function query() on a non-object in .../extensions/tasks.php on line 24

     

    Here is my updated php code:

    <?php
    try {
    $db = new PDO("mysql:host=localhost;dbname=dbsanbox", "root", "root"); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    }  
    catch(PDOException $e) {  
    
    echo $e->getMessage();
      
    }
    
    
    
    $userval = '2';
    
    class fetcher {
    
    private $pdo;
    
    function __contruct($db){
    	$this->pdo = $db;
    }
    
    function checkifDone( $value ){
    	if ( $value == 0 ) {
    		echo 'Not Done';
    	} elseif ( $value == 1 ){
    		echo 'Done';
    	}
    }
    
    function grab_tasks( $userid ){
    
    	$grabber = $this->pdo->query('SELECT * WHERE user_id =' . $userid);	
    	$grabber->setFetchMode(PDO::FETCH_OBJ);
    
    	while ( $row = $grabber->fetch() ){
    
    		$display = '<div>Task Name: ' . $row->name .'</div>';
    		$display .= '<div>Task Details: <p>'. $row->details  .'</p></div>';
    		//$display .= '<div>'. $this->checkifDone($row->task_status) .'</div>';
    		$display .= '<div>Task Due: '. $row->task_due . '</div>';
    		echo $display;
    	}
    }
    
    }
    
    
    $fetcher = new fetcher($db);
    $fetcher->grab_tasks($userval);
    
    
    ?>

     

  15. I'm a little bit confused about how I should reference it in my __contruct() from simply passing it on in the new fetcher() parameters.

     

    Its the first time I use PDO in an object and I'm wondering what is the most ideal and practical method in doing this correctly.

     

    Thank you very much!

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.